What is Bitwise Operators
Bitwise Operators
Unlike many other languages, C/C++ supports a full complement of bitwise operators. Since C was designed to take the place of assembly language for most programming tasks, it needed to be able to support many operations that can be done in assembler, including operations on bits.
Bitwise operation refers to testing, setting, or shifting the actual bits in a byte or word, which correspond to the char and int data types and variants. You cannot use bitwise operations on float, double, long double, void, bool, or other, more complex types. These operations are applied to the individual bits of the operands.
Operator Action
& AND
|OR
^ Exclusive OR (XOR)
~ One’s complement (NOT)
Bitwise Operators
The bitwise AND, OR, and NOT (one’s complement) are governed by the same truth table as their logical equivalents, except that they work bit by bit. The exclusive OR has the truth table shown here:
pqp^q
000
101
110
011
As the table indicates, the outcome of an XOR is true only if exactly one of the operands is true; otherwise, it is false.
Bitwise operations most often find application in device drivers—such as modem programs, disk file routines, and printer routines — because the bitwise operations can be used to mask off certain bits, such as parity. (The parity bit confirms that the rest of the bits in the byte are unchanged. It is usually the high-order bit in each byte.) Think of the bitwise AND as a way to clear a bit. That is, any bit that is 0 in either operand causes the corresponding bit in the outcome to be set to 0. For example, the following function reads a character from the modem port and resets the parity bit to 0:
char get_char_from_modem(void)
{
char ch;
ch = read_modem(); /* get a character from the
modem port */
return(ch & 127);
}
The bit-shift operators, >> and <<, move all bits in a variable to the right or left as specified. The general form of the shift-right statement is
variable >> number of bit positions
The general form of the shift-left statement is
variable << number of bit positions
As bits are shifted off one end, 0’s are brought in the other end. (In the case of a signed, negative integer, a right shift will causea1tobebrought in so that the sign bit is preserved.) Remember, a shift is not a rotate. That is, the bits shifted off one end do not come back around to the other. The bits shifted off are lost.
Bit-shift operations can be very useful when you are decoding input from an external device, like a D/A converter, and reading status information. The bitwise shift operators can also quickly multiply and divide integers. A shift right effectively divides a number by 2 and a shift left multiplies it by 2. The following program illustrates the shift operators:
/* A bit shift example. */
#include
int main(void)
{
unsigned int i;
int j;
i = 1;
/* left shifts */
for(j=0; j<4; j++) {
i = i << 1; /* left shift i by 1, which
is same as a multiply by 2 */
printf(”Left shift %d: %d\n”, j, i);
}
/* right shifts */
for(j=0; j<4; j++) { i = i >> 1; /* right shift i by 1, which
is same as a division by 2 */
printf(”Right shift %d: %d\n”, j, i);
}
return 0;
}
unsigned char x; x as each statement
value of x
executes
x=7; 00000111 7
x = x<<1; 00001110 14
x = x<<3; 01110000 112
x = x<<2; 11000000 192 x = x>>1; 01100000 96
x = x>>2; 00011000 24
*Each left shift multiplies by 2. Notice that information has been lost after x<<2 because a bit was shifted off the end.
**Each right shift divides by 2. Notice that subsequent divisions do not bring back any lost bits.
Multiplication and Division with Shift Operators
The one’s complement operator, ~, reverses the state of each bit in its operand. That is, all 1’s are set to 0, and all 0’s are set to 1.
The bitwise operators are often used in cipher routines. If you want to make a disk file appear unreadable, perform some bitwise manipulations on it. One of the simplest methods is to complement each byte by using the one’s complement to reverse each bit in the byte, as is shown here:
Original byte 00101100
After 1st complement 11010011
Same
After 2nd complement 00101100
Notice that a sequence of two complements in a row always produces the original number. Thus, the first complement represents the coded version of that byte. The second complement decodes the byte to its original value. You could use the encode()
function shown here to encode a character.
/* A simple cipher function. */
char encode(char ch)
{
return(~ch); /* complement it */
}
Of course, a file encoded using encode() would be very easy to crack!
Popularity: 1% [?]
Posted on December 14, 2009 at 2:56 pm
its a very nice article
Posted on December 13, 2009 at 9:06 am
sir can u explain all the operators please