E.g. x = 1001 0110 1100 1011
Would turn off the fourth bit in the variable y.
main()
{
char ch;
FILE *fp;
if( (fp=fopen(“data.dat”, “w”) == NULL)
{
printf(“File creation error”);
exit(1);
}
while( (ch=getchar())!=EOF )
fputc(~ch, fp);
fclose(fp);
}
Bitwise Logical Operators
There are three logical Bitwise operators:
Bitwise AND (&)
Bitwise OR (|) [ Bitwise inclusive OR]
Bitwise XOR (^) [ Bitwise exclusive OR]
Bitwise logical operators are binary operators and each of these operators requires two integer type operands.
The operations are carried out independently on each pair of corresponding bits within the operand. Thus, the least significant bits (i.e., rightmost bits) within the two operands will be compared, then the next least significant bit and so on, until all of the bits have been compared.
A single ampersand (&) character represents the Bitwise AND operator. The result of ANDing operation is 1 if both the bits have a value of 1 (true or ON); otherwise it is 0.
BIT 1
|
BIT 2
|
BIT 1 & BIT 2
|
0
|
0
|
0
|
0
|
1
|
0
|
1
|
0
|
0
|
1
|
1
|
1
|
E.g.
x 0000 0000 0000 1101 & /* decimal 13 */
y 0000 0000 0001 1001 /* decimal 25 */
x&y 0000 0000 0000 1001 /* decimal 9 */
Bitwise ANDing is used to test whether a particular bit is 1 or 0. This operator is combined with the one’s complement operator used to turn off a particular bit.
For example the following program tests whether the second bit of the variable flag is 1 or 0.
main()
{
int flag;
test=4; /* 0000 0000 0000 0100 */
printf(“Enter a number : “);
scanf(%d”, &flag);
if ( (flag & test) != 0)
{
printf(“The second bit of the entered number is set ( i.e., 1)”);
}
else
{
printf(“The second bit of the entered number is not set ( i.e., 0)”);
}
getch();
}
Q. Write a program to print the decimal number entered by the user is ODD or EVEN.
[Hint: The rightmost bit of an ODD number is always set to 1]
Ans:
main()
{
int test = 1; /* 0000 0000 0000 0001 */
int x;
printf(“Enter a number to test : “);
scanf(“%d”, &x);
if( x & test )
printf(“ Number is ODD ‘);
else
printf(“ Number is EVEN “);
}
Bitwise OR
The Bitwise OR is represented by the symbol | (vertical bar or pipe symbol). The result of OR operation is 1 if at least one of the bits has a value of 1 (true or ON); otherwise it is zero.
BIT 1
|
BIT 2
|
BIT 1 | BIT 2
|
0
|
0
|
0
|
0
|
1
|
1
|
1
|
0
|
1
|
1
|
1
|
1
|
E.g.
x 0000 0000 0000 1101 | /* decimal 13 */
y 0000 0000 0001 1001 /* decimal 25 */
x|y 0000 0000 0001 1101 /* decimal 29 */
The Bitwise OR operation is often used to set a particular bit to 1. For example,
main()
{
int set=8; /* 0000 0000 0000 1000 */
int x;
printf(“ Enter a number : “);
scanf( “ %d”, &x);
if( (x & set) ==0)
{
printf(“ the third bit is not set to 1”);
x = x | set ;
if( (x & set) !=0)
printf(“ Now the third bit is set to 1”);
}
else
printf(“ The third bit is already set to 1”);
getch();
}
The Bitwise eXclusive OR is represented by the symbol ^ (caret). The result of the XOR operator is 1 if only one of the bits is 1(true or ON); otherwise it is zero.
BIT 1
|
BIT 2
|
BIT 1 ^ BIT 2
|
0
|
0
|
0
|
0
|
1
|
1
|
1
|
0
|
1
|
1
|
1
|
0
|
E.g.
x 0000 0000 0000 1101 ^ /* decimal 13 */
y 0000 0000 0001 1001 /* decimal 25 */
x^y 0000 0000 0001 0100 /* decimal 22 */
The Bitwise XOR operator is used to toggle a bit ON or OFF. The number XORed with another number twice gives the original number.
main()
{
int x=50;
printf(“Before any XORing, the value of x = %d\n”, x); /* this will be 50 */
x ^= 12;
printf(“After the first XORing with 12, the value of x = %d\n”, x); /* this will be 62 */
x = x^12;
printf(“After the second XORing with 12, the value of x = %d\n”, x); /* this will be 50 */
}
Share with your friends: