To begin with it is necessary to speak about format of numbers notation on computers.
In our everyday life we’ve got used to the decimal notation of numbers: 56, 777, 10957 etc. Decimal notation of 10957 means that 10957 = 1104 +0103+9102+5101+7100. In other words a decimal number is a0*100+a1*101+a2*102+…+an*10n. I.e. the sum of products of the corresponding digit (ai) by the corresponding power of the number 10 (10i).
Computer hardware and software systems commonly use a binary representation, internally. In the binary representation numeric values are represented using two symbols 0 and 1, and the corresponding power of the number 2 and not the number 10 raised to the corresponding power is used as a factor. For example the binary number 10001101 is equal to a decimal (common) number 127+026+025+024+123+122+021+120 = 128+0+0+0+8+4+0+1 = 141.
Each such binary digit (1 or 0) is called a bit.
Now we know enough to consider bitwise operations.
Bitwise operators
One’s complement. In each position digits which were 0 become 1, and vice versa. Example:
int b = 141; // the initial value of the variable b equals 141,
// that in binary representation equals to 10001101
b = ~b; // the variable b became equal to 01110010, i.e. 114
Shift to the right. The binary representation of the firs operand is shifted to the right by the number of positions equal to the value of the second operand. The resulting empty “left” positions will be filled with zeros. Example:
int b = 141; // the initial value of the variable b is equal to 141,
// which in the binary representation is equal to 10001101
b = b >> 2; // the variable b became equal to 00100011, i.e. 67
Shift to the left. The binary representation of the firs operand is shifted to the left by the number of positions equal to the value of the second operand. The resulting “right” positions will be filled with zeros. Example:
int b = 141; // the initial value of the variable b is equal to 141,
// which in the binary representation is equal to 10001101
b = b << 2; // the variable b became equal to 1000110100, i.e. 564
Bitwise operator AND. The result will contain one in those positions where the corresponding positions of the first and the second operands contain one. Otherwise the corresponding bit of the result will be zero. Example:
int a = 25; // the initial value of the variable a is 25,
// which in the binary representation is equal to 00011001
int b = 141; // the initial value of the variable b is equal to 141,
// which on the binary representation is equal to 10001101
a = a & b; // the variable a became equal to 00001001, i.e. 9
Bitwise operator OR. The result will contain zero in those positions where the corresponding positions of the first and the second operand contain zero. Otherwise the corresponding bit of the result will be 1. Example:
int a = 25; // the initial value of the variable a is 25,
// which in the binary representation is equal to 00011001
int b = 141; // the initial value of the variable b is 141,
// which in the binary representation is equal to 10001101
a = a | b; // the variable a became equal to 10011101, i.e. 157
Bitwise operator exclusive OR. The result in each position is 1 if x and y have different binary values, and 0 if they are the same. Example:
int a = 25; // the initial value of the variable is 25,
// which in the binary representation is equal to 00011001
int b = 141; // the initial value of the variable b is equal to 141,
// which in the binary representation is equal to 10001101
a = a ^ b; // the variable a became equal to 10010100, i.e. 148
Next article: "
Conditional operator"