Objective: Understanding miscellaneous aspects of C ENUMERATED, STRUCTURE AND UNION TYPES: Derived types- structures declaration, definition and initialization of structures, accessing structures, nested structures, arrays of structures, structures and functions, pointers to structures, self referential structures, unions, typedef, bit-fields, program applications BIT-WISE OPERATORS: logical, shift, rotation, masks. C Programming Bitwise Operators: Bitwise operators are the operators which are used to perform operations at bit level. Bit Level Operations means the operations performed on individual bits of a given data. 1. Sometimes it become mandatory to consider data at bit level. 2. We have to operate on the individual data bit. We also need to turn on/off particular data bit during source code drafting, at that time we must use bitwise operator for doing our task easier. 3. C Programming provides us different bitwise operators for manipulation of bits. 4. Bitwise operators operates on Integer, character but not on float, double 5. Using bitwise operators we can manipulate individual bits easily C programming supports 6 bitwise operators Operator Name of Operator ~ Not operator (or) One s Compliment >> Right Shift << Left Shift & Bitwise AND Bitwise OR ^ Bitwise XOR Bitwise operator rules Input Bits AND OR XOR 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0 Page 1 of 5
Bitwise Operators : AND,OR,XOR AND,OR,XOR are three main Bitwise Operators in C Programming Language Example : Consider two numbers 12 and 10 a = 12 b = 10 a in Binary : 0000 0000 0000 1100 (means 12) b in Binary : 0000 0000 0000 1010 (means 10) a b : 0000 0000 0000 1110 a& b : 0000 0000 0000 1000 a ^ b : 0000 0000 0000 0110 Rules from above table : 1. If Two bits are same Then Resultant XOR is 0. 2. If Two bits are different Then Resultant XOR is 1. 3. If any of the bit is 1 then Resultant OR is 1 4. If both bits are 0 then Resultant OR is 0 5. If any of the bit is 0 then Resultant AND is 0. a b : 0000 0000 0000 1110 = 14 a& b : 0000 0000 0000 1000 = 8 a ^ b : 0000 0000 0000 0110 = 6 Example : Bitwise Operator (AND,OR,XOR) int a=12,b=10; printf("\nnumber1 AND Number2 : %d",a& b); printf("\nnumber OR Number2 : %d",a b); printf("\nnumber XOR Number2 : %d",a ^ b); Number1 AND Number2 : 8 Number OR Number2 : 14 Number XOR Number2 : 6 Page 2 of 5
Not (or)one s Compliment Operator in C 1. It is denoted by ~ 2. Bit Pattern of the data can be Reversed using One s Compliment 3. It inverts each bit of operand. One s Compliment is Unary Operand i.e Operates on 1 Argument One scompliment 1111 1111 1100 0011 Zero s Are Changed to 1 One s Are Changed to 0 Syntax : ~Variable_Name Live Example : Negation Operator in C Programming int a=10; printf("\nnegation of Number 1 : %d",~a); Negation of Number 1 : -11 Page 3 of 5
Bitwise Right Shift Operator in C It is denoted by >> Bit Pattern of the data can be shifted by specified number of Positions to Right When Data is Shifted Right, leading zero s are filled with zero. Right Shift by 2 0000 0000 0000 1111 Leading 2 Blanks Replaced by 0 Syntax : [variable]>>[number of places] int a = 10; printf("\nnumber is Shifted By 1 Bit : %d",a>> 1); printf("\nnumber is Shifted By 2 Bits : %d",a>> 2); Number is Shifted By 1 Bit : 5 Number is Shifted By 2 Bits : 2 Explanation: We know the binary representation of the 10 is as below 0000 0000 0000 1010 Now after shifting all the bits to left towards MSB we will get following bit pattern 0000 0000 0000 0101 = 5 For >> 1 ------------------- 0000 0000 0000 0010 = 2 For <<2 Page 4 of 5
Bitwise Left Shift Operator in C It is denoted by << Bit Pattern of the data can be shifted by specified number of Positions to Left When Data is Shifted Left, trailing zero s are filled with zero. Left Shift 0000 0000 1111 0000 Trailing Zero s Replaced by 0 (Shown in RED) Syntax : Bitwise Left Shift Operator [variable]<<[number of places] int a = 10; printf("\nnumber is Shifted By 1 Bit : %d",a<< 1); printf("\nnumber is Shifted By 2 Bits : %d",a<< 2); Number is Shifted By 1 Bit : 20 Number is Shifted By 2 Bits : 40 Explanation: We know the binary representation of the 10 is as below 0000 0000 0000 1010 Now after shifting all the bits to left towards MSB we will get following bit pattern 0000 0000 0001 0100 = 20 For << 1 ------------------- 0000 0000 0010 1000 = 40 For <<2 Page 5 of 5