Introduction of Bitwise operator - WordPress.com

4 downloads 640 Views 478KB Size Report
FACULTY OF COMPUTER APPLICATIONS. BITWSIE ... important that bitwise operators only work on two data types : int and char. Bitwise operators fall into two ...
MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS BITWSIE OPERATORS PREPROCESSOR STSTEMENTS

SUB : FOP (610001) (MCA – I)

Introduction of Bitwise operator Since a computer understand only machine language , data is represented as binary numbers that are nothing but various combinations of 0’s and 1’s. Bitwise operators allow one to read and manipulate bits in variables of certain data types. It is to be important that bitwise operators only work on two data types : int and char. Bitwise operators fall into two categories : (1) Binary bitwise operators (2) Unary bitwise operators. Binary bitwise operators take two arguments while unary operators only take one or we can say operand instead of argument. The ~ (Bitwise NOT) is a unary bitwise operator as it acts on a single operand. The & , | and ^ are known as bitwise logical operators. The >> and Bitwise shift right Precedence of bitwise operators

1

Prepared By : Ashwin R. Dobariya

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS BITWSIE OPERATORS PREPROCESSOR STSTEMENTS

SUB : FOP (610001) (MCA – I)

Uses of Bitwise Operators • Occasionally, such an operation is needed to implement a large number of Boolean variables without using a lot of space. • A 32 bit int can be used to store 32 Boolean variables. Normally the minimum size of one Boolean variable is one bit. All types in C must have sizes that are multiples of bytes. However, only one bit is necessary to represent a Boolean value. • Bits can also be used to represent elements of a (small) set. If bit is 1 then element I is set, otherwise it is not. • Bitwise AND can be used to implement set – intersection. • Bitwise OR can be used to implement set – union. Details about bitwise operators with examples (1) Bitwise AND (&) Operator & OR ( | ) Operator The bitwise AND is true only if both bits are set (or 1). The following table defines & by applying AND on individual bits. The bitwise OR returns a 1 if either of the tow bits is a 1 or one of the bit is 1. X 0 0 1 1 However, here is an example bits. Variable Value X 12 Y 10 Z=x&y 8 z=x|Y 14

Y X&Y X|Y 0 0 0 1 0 1 0 0 1 1 1 1 of bitwise & applied on numbers represented by four b3 1 1 1 1

b2 1 0 0 1

b1 0 1 0 1

b0 0 0 0 0

Consider another following example : int a,b,c; a = 12; b = 8; c = a & b; 2

Prepared By : Ashwin R. Dobariya

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS BITWSIE OPERATORS PREPROCESSOR STSTEMENTS

SUB : FOP (610001) (MCA – I)

The bitwise AND operation will be performed at the bit level as follows: a = 12 ‐‐‐ 00001100 b = 8 ‐‐‐‐ 00001000 & 00001000 Then the variable “c” will hold the result of bitwise AND operation between “a” and “b” which is 000010002 (810). For example : The & operator can be used to check whether a number is power of 2 or not. ( 8 is a power no. but 12 is not) #include #include void main() { int n; clrscr(); printf("\n Enter any number : "); scanf("%d",&n); if((n & (n‐1)) == 0) printf("\n The number is power of 2"); else printf("\n The number is not power of 2"); getch(); } (2) Bitwise Exclusive – OR operator ( ^ ) The ^ operator returns 1 if either of the two bits (but not both) is 1. The following truth table displays concept of Ex‐OR operator. X Y X^Y 0 0 0 0 1 1 1 0 1 1 1 0

3

Prepared By : Ashwin R. Dobariya

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS BITWSIE OPERATORS PREPROCESSOR STSTEMENTS

SUB : FOP (610001) (MCA – I)

For example : Using bitwise operator, two integer variables can be swapped without using the third variable. The following program illustrate this concept. #include #include void main() { int a,b; clrscr(); printf("\n Enter value of a : "); scanf("%d",&a); printf("\n Enter value of b : "); scanf("%d",&b); a^=b^=a^=b; printf("\n Value of a is : %d",a); printf("\n Value of b is : %d",b); getch(); } Example : 2. Write a program to input a character in any case and convert it in opposite case using XOR operator. #include void main() { char ch; clrscr(); printf("Character case conversion: "); printf("\n Enter a character in any case uper or lower :"); scanf("%c",&ch); //read character printf("Converted in opposite case : %c", ch ^ 32); //input XOR 32 getch(); } 4

Prepared By : Ashwin R. Dobariya

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS BITWSIE OPERATORS PREPROCESSOR STSTEMENTS

SUB : FOP (610001) (MCA – I)

(3) Bitwise NOT operator There is only one unary bitwise operator – NOT operator. It is also known as 1’s complement operator. Bitwise NOT flips all the bits. This works on a single operand and simply converts each 1 to 0 and 0 to 1. For example : #include #include Void main() { Int num = 0xFFFF; Printf(“\n The complement of %x is %x “,num,~num); } (4) Bitwise shift operator Shift operators allow shifting of bits either to left or right. We know that 8 is represented in binary as: 8 = 00001000 and when it is shifted by one bit to right, we get: 8 = 00001000 ‐Æ 00000100 = 4 Similarly when the number is shifted by one bit to left, we get: 16 = 00010000 Å 00001000 = 8 The C right shift (>>) operator shifts an 8‐bit number by one or more bits to right while left shift ( number‐of‐bits Left shift is given in the form: variable > 2

5

Prepared By : Ashwin R. Dobariya

MARWADI EDUCATION FOUNDATION’S GROUP OF INSTITUTIONS (MEFGI) FACULTY OF COMPUTER APPLICATIONS BITWSIE OPERATORS PREPROCESSOR STSTEMENTS

SUB : FOP (610001) (MCA – I)

For example : The following program converts decimal number into binary digits using bitwise operator. #include #include void main() { int n,i,k,m; clrscr(); printf("\n Enter any number : "); scanf("%d",&n); for(i=15;i>=0;i‐‐) { m = 1