Free Hosting

Bitwise operators


Bit manipulation operators manipulate individual bits within a variable.
Why bother with bitwise operators?
In the past, memory was extremely expensive, and computers did not have much of it. Consequently, there were incentives to make use of every bit of memory available. Consider the bool data type — even though it only has two possible values (true and false), which can be represented by a single bit, it takes up an entire byte of memory! This is because variables need unique addresses, and memory can only be addressed in bytes. The bool uses 1 bit and the other 7 go to waste.
Using bitwise operators, it is possible to write functions that allow us to compact 8 booleans into a single byte-sized variable, enabling significant memory savings at the expense of more complex code. In the past, this was a good trade-off. Today, it is not.
Now memory is significantly cheaper, and programmers have found that it is often a better idea to code what is easiest to understand and maintain than what is most efficient. Consequently, bitwise operators have somewhat fallen out of favor, except in certain circumstances where maximum optimization is needed (eg. scientific programs that use enormous data sets, or games where bit manipulation tricks can be used for extra speed). Nevertheless, it is good to at least know about their existence.
There are 6 bit manipulation operators:
OperatorSymbolFormOperation
left shift<<x << yall bits in x shifted left y bits
right shift>>x >> yall bits in x shifted right y bits
bitwise NOT~~xall bits in x flipped
bitwise AND&x & yeach bit in x AND each bit in y
bitwise OR|x | yeach bit in x OR each bit in y
bitwise XOR^x ^ yeach bit in x XOR each bit in y
Note: In the following examples, we will largely be working with 4-bit binary values. This is for the sake of convenience and keeping the examples simple. In C++, the number of bits used will be based on the size of the data type (8 bits per byte).
Left shift and right shift operator
The bitwise left shift (<<) shifts operator bits to the left. For example, consider the number 3, which is binary 0011. Left shifting by 1 (3 << 1) changes 0011 to 0110, which is decimal 6. Note how each bit moved 1 place to the left. Left shifting by 2 (3 << 2) changes 0011 to 1100, which is decimal 12. Left shifting by 3 (3 << 3) changes 0011 to 1000. Note that we shifted a bit off the end of the number! Bits that are shifted off the end of the binary number are lost.
The bitwise right shift (>>) operator shifts bits to the right. Right shifting by 1 (3 >> 1) changes 0011 to 0001, or decimal 1. The rightmost bit shifted off the end and was lost!
Although our examples above are shifting literals, you can shift variables as well:
1
2
unsigned int nValue = 4;
nValue = nValue << 1; // nValue will be 8
Rule: When dealing with bit operators, use unsigned variables.
Programs today typically do not make much use of the bitwise left and right shift operator in this capacity. Rather, you tend to see the bitwise left shift operator used with cout in a way that doesn’t involve shifting bits at all! If << is a bitwise left shift operator, then how does cout << "Hello, world!"; print to the screen? The answer is that cout has overridden (replaced) the default meaning of the << operator and given it a new meaning. We will talk more about operator overloading in a future section!
Bitwise NOT
The bitwise NOT operator (~) is perhaps the easiest to understand of all the bitwise operators. It simply flips each bit from a 0 to a 1, or vice versa. Note that the result of a bitwise NOT is dependent on what size your data type is! For example, with 4 bits, ~4 (0100 binary) evaluates to 1011, which is 11 in decimal. In an 8 bit data type (such as an unsigned char), ~4 (represented as ~0000 0100) evaluates to 1111 1011, which is 251 in decimal!
Bitwise AND, OR, and XOR
Bitwise AND (&) and bitwise OR (|) work similarly to their logical AND and logical OR counterparts. However, rather than evaluating a single boolean value, they are applied to each bit! For example, consider the expression 5 | 6. In binary, this is represented as 0101 | 0110. To do (any) bitwise operations, it is easiest to line the two operands up like this:
0 1 0 1 // 5
0 1 1 0 // 6
and then apply the operation to each column of bits. If you remember, logical OR evaluates to true (1) if either the left or the right or both operands are true (1). Bitwise OR evaluates to 1 if either bit (or both) is 1. Consequently, 5 | 6 evaluates like this:
0 1 0 1 // 5
0 1 1 0 // 6
-------
0 1 1 1 // 7
Our result is 0111 binary (7 decimal).
We can do the same thing to compound OR expressions, such as 1 | 4 | 6. If any of the bits in a column are 1, the result of that column is 1.
0 0 0 1 // 1
0 1 0 0 // 4
0 1 1 0 // 6
--------
0 1 1 1 // 7
1 | 4 | 6 evaluates to 7.
Bitwise AND works similarly. Logical AND evaluates to true if both the left and right operand evaluate to true. Bitwise AND evaluates to true if both bits in the column are 1) Consider the expression 5 & 6. Lining each of the bits up and applying an AND operation to each column of bits:
0 1 0 1 // 5
0 1 1 0 // 6
--------
0 1 0 0 // 4
Similarly, we can do the same thing to compound AND expressions, such as 1 & 3 & 7. If all of the bits in a column are 1, the result of that column is 1.
0 0 0 1 // 1
0 0 1 1 // 3
0 1 1 1 // 7
--------
0 0 0 1 // 1
The last operator is the bitwise XOR (^), also known as exclusive or. When evaluating two operands, XOR evaluates to true (1) if one and only one of it's operands is true (1). If neither or both are true, it evaluates to 0. Consider the expression 6 ^ 3:
0 1 1 0 // 6
0 0 1 1 // 3
-------
0 1 0 1 // 5
It is also possible to evaluate compound XOR expression column style, such as 1 ^ 3 ^ 7. If there are an even number of 1 bits in a column, the result is 0. If there are an odd number of 1 bits in a column, the result is 1.
0 0 0 1 // 1
0 0 1 1 // 3
0 1 1 1 // 7
--------
0 1 0 1 // 5
Bitwise assignment operators
As with the arithmetic assignment operators, C++ provides bitwise assignment operators in order to facilitate easy modification of variables.
OperatorSymbolFormOperation
Left shift assignment<<=x <<= yShift x left by y bits
Right shift assignment>>=x >>= yShift x right by y bits
Bitwise OR assignment|=x |= yAssign x | y to x
Bitwise AND assignment&=x &= yAssign x & y to x
Bitwise XOR assignment^=x ^= yAssign x ^ y to x
For example, instead of writing nValue = nValue << 1;, you can write nValue <<= 1;.
Summary
Summarizing how to evaluate bitwise operations utilizing the column method:
When evaluating bitwise OR, if any bit in a column is 1, the result for that column is 1.
When evaluating bitwise AND, if all bits in a column are 1, the result for that column is 1.
When evaluating bitwise XOR, if there are an odd number of 1 bits in a column, the result for that column is 1.
Quiz
1) What does 0110 >> 2 evaluate to in binary?
2) What does 5 | 6 evaluate to in decimal?
3) What does 5 & 6 evaluate to in decimal?
4) What does 5 ^ 6 evaluate to in decimal?
Quiz answers
4) Show SolutionBit manipulation operators manipulate individual bits within a variable.
Why bother with bitwise operators?
In the past, memory was extremely expensive, and computers did not have much of it. Consequently, there were incentives to make use of every bit of memory available. Consider the bool data type — even though it only has two possible values (true and false), which can be represented by a single bit, it takes up an entire byte of memory! This is because variables need unique addresses, and memory can only be addressed in bytes. The bool uses 1 bit and the other 7 go to waste.
Using bitwise operators, it is possible to write functions that allow us to compact 8 booleans into a single byte-sized variable, enabling significant memory savings at the expense of more complex code. In the past, this was a good trade-off. Today, it is not.
Now memory is significantly cheaper, and programmers have found that it is often a better idea to code what is easiest to understand and maintain than what is most efficient. Consequently, bitwise operators have somewhat fallen out of favor, except in certain circumstances where maximum optimization is needed (eg. scientific programs that use enormous data sets, or games where bit manipulation tricks can be used for extra speed). Nevertheless, it is good to at least know about their existence.
There are 6 bit manipulation operators:
OperatorSymbolFormOperation
left shift<<x << yall bits in x shifted left y bits
right shift>>x >> yall bits in x shifted right y bits
bitwise NOT~~xall bits in x flipped
bitwise AND&x & yeach bit in x AND each bit in y
bitwise OR|x | yeach bit in x OR each bit in y
bitwise XOR^x ^ yeach bit in x XOR each bit in y
Note: In the following examples, we will largely be working with 4-bit binary values. This is for the sake of convenience and keeping the examples simple. In C++, the number of bits used will be based on the size of the data type (8 bits per byte).
Left shift and right shift operator
The bitwise left shift (<<) shifts operator bits to the left. For example, consider the number 3, which is binary 0011. Left shifting by 1 (3 << 1) changes 0011 to 0110, which is decimal 6. Note how each bit moved 1 place to the left. Left shifting by 2 (3 << 2) changes 0011 to 1100, which is decimal 12. Left shifting by 3 (3 << 3) changes 0011 to 1000. Note that we shifted a bit off the end of the number! Bits that are shifted off the end of the binary number are lost.
The bitwise right shift (>>) operator shifts bits to the right. Right shifting by 1 (3 >> 1) changes 0011 to 0001, or decimal 1. The rightmost bit shifted off the end and was lost!
Although our examples above are shifting literals, you can shift variables as well:
1
2
unsigned int nValue = 4;
nValue = nValue << 1; // nValue will be 8
Rule: When dealing with bit operators, use unsigned variables.
Programs today typically do not make much use of the bitwise left and right shift operator in this capacity. Rather, you tend to see the bitwise left shift operator used with cout in a way that doesn’t involve shifting bits at all! If << is a bitwise left shift operator, then how does cout << "Hello, world!"; print to the screen? The answer is that cout has overridden (replaced) the default meaning of the << operator and given it a new meaning. We will talk more about operator overloading in a future section!
Bitwise NOT
The bitwise NOT operator (~) is perhaps the easiest to understand of all the bitwise operators. It simply flips each bit from a 0 to a 1, or vice versa. Note that the result of a bitwise NOT is dependent on what size your data type is! For example, with 4 bits, ~4 (0100 binary) evaluates to 1011, which is 11 in decimal. In an 8 bit data type (such as an unsigned char), ~4 (represented as ~0000 0100) evaluates to 1111 1011, which is 251 in decimal!
Bitwise AND, OR, and XOR
Bitwise AND (&) and bitwise OR (|) work similarly to their logical AND and logical OR counterparts. However, rather than evaluating a single boolean value, they are applied to each bit! For example, consider the expression 5 | 6. In binary, this is represented as 0101 | 0110. To do (any) bitwise operations, it is easiest to line the two operands up like this:
0 1 0 1 // 5
0 1 1 0 // 6
and then apply the operation to each column of bits. If you remember, logical OR evaluates to true (1) if either the left or the right or both operands are true (1). Bitwise OR evaluates to 1 if either bit (or both) is 1. Consequently, 5 | 6 evaluates like this:
0 1 0 1 // 5
0 1 1 0 // 6
-------
0 1 1 1 // 7
Our result is 0111 binary (7 decimal).
We can do the same thing to compound OR expressions, such as 1 | 4 | 6. If any of the bits in a column are 1, the result of that column is 1.
0 0 0 1 // 1
0 1 0 0 // 4
0 1 1 0 // 6
--------
0 1 1 1 // 7
1 | 4 | 6 evaluates to 7.
Bitwise AND works similarly. Logical AND evaluates to true if both the left and right operand evaluate to true. Bitwise AND evaluates to true if both bits in the column are 1) Consider the expression 5 & 6. Lining each of the bits up and applying an AND operation to each column of bits:
0 1 0 1 // 5
0 1 1 0 // 6
--------
0 1 0 0 // 4
Similarly, we can do the same thing to compound AND expressions, such as 1 & 3 & 7. If all of the bits in a column are 1, the result of that column is 1.
0 0 0 1 // 1
0 0 1 1 // 3
0 1 1 1 // 7
--------
0 0 0 1 // 1
The last operator is the bitwise XOR (^), also known as exclusive or. When evaluating two operands, XOR evaluates to true (1) if one and only one of it's operands is true (1). If neither or both are true, it evaluates to 0. Consider the expression 6 ^ 3:
0 1 1 0 // 6
0 0 1 1 // 3
-------
0 1 0 1 // 5
It is also possible to evaluate compound XOR expression column style, such as 1 ^ 3 ^ 7. If there are an even number of 1 bits in a column, the result is 0. If there are an odd number of 1 bits in a column, the result is 1.
0 0 0 1 // 1
0 0 1 1 // 3
0 1 1 1 // 7
--------
0 1 0 1 // 5
Bitwise assignment operators
As with the arithmetic assignment operators, C++ provides bitwise assignment operators in order to facilitate easy modification of variables.
OperatorSymbolFormOperation
Left shift assignment<<=x <<= yShift x left by y bits
Right shift assignment>>=x >>= yShift x right by y bits
Bitwise OR assignment|=x |= yAssign x | y to x
Bitwise AND assignment&=x &= yAssign x & y to x
Bitwise XOR assignment^=x ^= yAssign x ^ y to x
For example, instead of writing nValue = nValue << 1;, you can write nValue <<= 1;.
Summary
Summarizing how to evaluate bitwise operations utilizing the column method:
When evaluating bitwise OR, if any bit in a column is 1, the result for that column is 1.
When evaluating bitwise AND, if all bits in a column are 1, the result for that column is 1.
When evaluating bitwise XOR, if there are an odd number of 1 bits in a column, the result for that column is 1.
Quiz
1) What does 0110 >> 2 evaluate to in binary?
2) What does 5 | 6 evaluate to in decimal?
3) What does 5 & 6 evaluate to in decimal?
4) What does 5 ^ 6 evaluate to in decimal?
Quiz answers

0 comments:

Blogger Template by Clairvo