Join the Discussion
The group of operators that I left out are the bitwise operators. In order to understand what they do you need to have an understanding of how computers store information so we'll start there. If you already know about bits and bytes feel free to skip ajead.
Computers don't actually understand characters or even numbers. All they understand is on and off. A given location in memory can either be turned on or turned off. To allow us to get computers to work with numbers we treat these on and off signals as binary numbers and refer to them as 1 when it is on and 0 when it is off. We can then translate these binary numbers into the decimal numbers that we are more familiar with and can thereforwe get the computer to easily handle numbers even though it doesn't know what numbers are.
To get the computer to handle characters other than numbers we need to allocate a specific number to represent each of those characters. There are several different ways that computers map the characters. Popular character encoding methods are EBCDIC, ASCII, and unicode. The first two of these allow for up to 256 different characters and the number of binary digits (usually abbreviated to bits) needed to store 256 different values is 8. Most computers therefore work with bytes of memory each of which consists of eight bits. Unicode uses between one and four bytes to represent just about any possible character in any possible language.
- setting up multiple flags in a much smaller space - bit operators work with 31 bit signed numbers and so 32 flags can be stored in those 4 bytes instead of requiring one byte each
- manipulating hexadecimal fields such as IP addresses and colour codes
- exchanging the values in two fields without requiring any temporary fields
So what bitwise operators are there and what do they do?
- & is the bitwise AND operator. There are four possible outcomes from a comparison involving this operator as illustrated in the and table below. By setting up the second value as a mask you can zero out the specific bits in the first value that you have set to zero in the second one.
- | is the bitwise OR operator. Again, there are four possible outcomes from a comparison involving this operator as illustrated in the or table below. By setting up the second value as a mask you can convert the specific bits in the first value that you have set to one in the second to also be one.
- ^ is the bitwise exclusive OR operator. Again four possible outcomes as illustrated in the xor table below. By setting up the second value as a mask you can invert selected bits in the first.
- ~ is the bitwise ones complement operator. It takes just one value and will invert each bit within that value. All of the 0 will become 1 and the 1 will become 0. A fast way of changing the sign of a number would be to use this operator on the field and then add one to the result.
- << is the bitwise left shift operator and moves all of the bits one to the left. The leftmost of the 32 bits is dropped and a 0 is added to the end.
- >> is the bitwise sign propogating right shift operator. All bits are moved one to the right with the rightmost being dropped. The new bit added to the left of the value will have the same value as that immediately to its right.
- >>> is the bitwise zero fill right shift operator. All bits are moved one to the right with the right most being dropped. A zero bit is added to the left end.
These operators can also be combined with = to make a bitwise assignment operator. This is particularly useful sometimes for those operators that require two fields rather than just one (& | ^) where the result is to be loaded into one of those two fields. For example A |= B is equivalent to A = A | B.
Oh, and if you are wondering which of these operators you would use to exchange the contents of two fields without the use of a temporary field the answer is to se three exclusive ors. To swap A and B you would use:
B ^= A;
A ^= B;