The bitwise operators operate on the bits in integer values.
There are six bitwise operators, as shown in the following Table.
Operator | Description |
---|---|
& | Bitwise AND operator |
| | Bitwise OR operator |
^ | Bitwise Exclusive OR (XOR) operator |
~ | Bitwise NOT operator, also called the 1's complement operator |
>> | Bitwise shift right operator |
<< | Bitwise shift left operator |
All of these only operate on integer types.
The ~
operator is a unary operator-it applies to one operand-and the
others are binary operators.
The bitwise AND operator, &
,
combines the corresponding bits of its operands in such a way that if both bits are 1,
the resulting bit is 1; otherwise, the resulting bit is 0.
Suppose you declare the following variables:
int x = 13;
int y = 6;
int z = x & y; // AND corresponding bits of x and y
After the third statement, z will have the value 4 (binary 100).
This is because the corresponding bits in x and y are combined as follows:
x 0 0 0 0 1 1 0 1 y 0 0 0 0 0 1 1 0 x & y 0 0 0 0 0 1 0 0
Example
#include <stdio.h>
//from w ww . j a v a 2s . co m
int main(void)
{
int x = 13;
int y = 6;
int z = x & y; // AND corresponding bits of x and y
printf("\n original = %X", x);
printf("\n original = %X", y);
printf("\t result = %X\n", z);
return 0;
}
The code above generates the following result.
The bitwise OR operator, |
, results in 1 if either or both of
the corresponding bits are 1; otherwise, the result is 0.
If you combine the same values of x and y using the | operator in a statement such as this:
int x = 13;
int y = 6;
int z = x | y; // OR the bits of x and y
x 0 0 0 0 1 1 0 1
y 0 0 0 0 0 1 1 0
x | y 0 0 0 0 1 1 1 1
The value stored in z would therefore be 15 (binary 1111).
Example
#include <stdio.h>
/*from w w w .j av a 2 s.co m*/
int main(void)
{
int x = 13;
int y = 6;
int z = x | y; // OR the bits of x and y
printf("\n original = %X", x);
printf("\n original = %X", y);
printf("\t result = %X\n", z);
return 0;
}
The code above generates the following result.
The bitwise XOR operator, ^
, produces a 1 if both bits are different,
and 0 if they're the same.
int x = 13; int y = 6; int z = x ^ y; // Exclusive OR the bits of x and y <myPreCode> <p>This results in z containing the value 11 (binary 1011), because the bits combine as follows: </p> <myPreCode> x 0 0 0 0 1 1 0 1 y 0 0 0 0 0 1 1 0 x ^ y 0 0 0 0 1 0 1 1
Example
#include <stdio.h>
/*www. j a v a 2s.co m*/
int main(void)
{
int x = 13;
int y = 6;
int z = x ^ y; // Exclusive OR the bits of x and y
printf("\n original = %X", x);
printf("\n original = %X", y);
printf("\t result = %X\n", z);
return 0;
}
The code above generates the following result.
The unary operator, ~
, flips the bits of its operand, so 1 becomes 0, and 0 becomes 1.
int x = 13; int z = ~x; // Store 1's complement of x
After executing this statement, z will have the value 14. The bits are set as follows:
x 0 0 0 0 1 1 0 1 ~x 1 1 1 1 0 0 1 0
The value 1111 0010 is 14 in two's complement representation of negative integers.
Example
#include <stdio.h>
/*from ww w .j a v a 2 s . c om*/
int main(void)
{
int x = 13;
int z = ~x; // Store 1's complement of x
printf("\n original = %X", x);
printf("\t result = %X\n", z);
return 0;
}
The code above generates the following result.
The shift operators shift the bits in the left operand by the number of positions specified by the right operand.
You could specify a shift-left operation with the following statements:
int value = 12; int shiftcount = 3; // Number of positions to be shifted int result = value << shiftcount; // Shift left shiftcount positions
The bits are shifted to the left three positions, and 0s are introduced on the right.
Example
#include <stdio.h>
/*from w ww. ja v a2 s. co m*/
int main(void)
{
int value = 12;
int shiftcount = 3; // Number of positions to be shifted
int result = value << shiftcount; // Shift left shiftcount positions
printf("\t result = %X\n", result);
return 0;
}
The code above generates the following result.
The right shift operator moves the bits to the right. For unsigned values, the bits that are introduced on the left are filled with zeros.
unsigned int value = 65372U; unsigned int result = value >> 2; // Shift right two bits
The bits in value will be shifted two places to the right, introducing zeros at the left end.
Example
#include <stdio.h>
/*w w w. ja v a 2 s .co m*/
int main(void)
{
unsigned int value = 65372U;
unsigned int result = value >> 2; // Shift right two bits
printf("\t result = %X\n", result);
return 0;
}
The code above generates the following result.
For signed values that are negative, the leftmost bit will be 1, and the result of a right shift depends on your system.
int new_value = -164; int new_result = new_value >> 2; // Shift right two bits
This will shift the value in new_value two bit positions to the right,
and the result will be stored in new_result
.
Example
#include <stdio.h>
/* w w w . ja v a 2s . c om*/
int main(void)
{
int new_value = -164;
int new_result = new_value >> 2; // Shift right two bits
printf("\t result = %X\n", new_result);
return 0;
}
The code above generates the following result.
You can use all of the binary bitwise operators in the op= form of assignment.
The exception is the operator ~
, which is
a unary operator.
lhs op= rhs;
is equivalent to the statement:
lhs = lhs op (rhs);
This means that if you write:
value <<= 4;
The effect is to shift the contents of the integer variable, value, left four bit positions.
It's exactly the same as the following:
value = value << 4;
This example illustrates how you can use a mask to select multiple bits from a variable.
You'll write a program that sets a value in a variable and then uses the bitwise operators to reverse the sequence of hexadecimal digits.
#include <stdio.h>
// w w w . j av a 2s . co m
int main(void)
{
unsigned int original = 0xAAA;
unsigned int result = 0;
unsigned int mask = 0xF; // Rightmost four bits
printf("\n original = %X", original);
// Insert first digit in result
result |= original & mask; // Put right 4 bits from original in result
// Get second digit
original >>= 4; // Shift original right four positions
result <<= 4; // Make room for next digit
result |= original & mask; // Put right 4 bits from original in result
/* Get third digit */
original >>= 4; // Shift original right four positions
result <<= 4; // Make room for next digit
result |= original & mask; // Put right 4 bits from original in result
printf("\t result = %X\n", result);
return 0;
}
The code above generates the following result.