I don't under stand what the << does as in 2<<16. If you could explain it I'd be vary thankful :)
Printable View
I don't under stand what the << does as in 2<<16. If you could explain it I'd be vary thankful :)
That's a bitwise left shift, and basically your expression can be seen as:
2 * 2**16, where ** denotes exponentiation.
In that context, << is called "a bitwise shift operator", which operates on numbers that are written in "binary format". Therefore, you have to understand what a number written in binary format is. First, let's take a look at the more common decimal format you are familiar with. Here is a number in decimal format:
146
The right most column is the 1's column, and there are six 1's in the number. The next column to the left is the 10's column and there are four 10's in the number. The next column to the left of that is the 100's column and there is one 100 in the number. There is a pattern: the right most column is 10^0 = 1's, then next column to the left is 10^1 = 10's, and the next column to the left of that is 10^2 = 100's. "Deci" is Latin for 10, hence the name "decimal" number system.
Binary format uses the binary number system. In the binary number system, if you have a number like this:
101
the right most column is the number of 1's, the next column to the left is the number of 2's, and the next column to the left is the number of 4's. So, the binary number:
is equal to 5. Just like with decimal numbers, there is a pattern: 2^0 = 1, and 2^1 = 2, and 2^2 = 4.Code:1 0 1
4's 2's 1's
So, how would you write the number 10 in binary format? First, write down the binary column values starting with the 1's column on the right. To figure out the next column to the left, double the previous column:
Once the left most column value is higher than the number, e.g. 10 in this example, you can stop. Next, start from the left, and fill in the number of each column value present in the number 10. There is one 8 in 10, so fill that in:Code:16's 8's 4's 2's 1's
Now, move one column to the right. How many 4's are there in 10 after you subtract the one 8 we have already accounted for? Zero, so you continue like this:Code:1
8's 4's 2's 1's
Move one column to the right again. How many 2's are there in 10 after accounting for the 8? There is one 2, so we can write:Code:1 0
8's 4's 2's 1's
Since we have accounted for an 8 and a 2, which adds up to 10, there aren't any ones, and we can write:Code:1 0 1
8's 4's 2's 1's
So, 10 written in binary format is:Code:1 0 1 0
8's 4's 2's 1's
1010
Now, lets examine what 2<<16 does. That statement says to take the number 2 written in binary format, and shift all the digits 16 columns to the left. Since 2 written in binary format looks like this(with a bunch of leading 0's):
0000 0000 0000 0000 0010
shifting the digts 16 places to the left yields this:
0010 0000 0000 0000 0000
which is equal to? You can start at the right and count up the columns: 1's, 2's, 4's, 8's and keep doubling the previous column until you get to the column with the 1 in it. The 1 means there is 1 of that column value in the number. Or, much more simply: each column is a power of 2 starting with 0, so if you count the columns starting at the right, which is made easier since I've grouped them into sets of 4, you count: 4, 8, 12, 16 plus 2 more columns in the last set, which gives you a 1 in the 18th column from the right. Since the first column is 2^0, the 18th column is 2^17. The power you raise 2 to is just the number of columns you counted minus 1.
So, 2<<16 is equal to 2^17 = 131,072
Check-out the Bit Shift FAQ.
Some comments about bit-shifting...
- Bit-shifting and bitwise operations are not very common, unless you're a hardware guy like me.... I use this stuff every day. But if this is for a class, you'd better learn it NOW! :)
- Sometimes it's used a a quick way to multiply or divide by two (or powers of two). Integers only... Dont' try this with a float!
- When you are working with binary, you are usually thinking in terms of "bit patterns" rather than "numbers". Usually one bit represents the state of something... For example, if an LED is on or off. For hardware troubleshooting, I'll often write 55 (hex) or AA (hex). These are handy bit-patterns for finding bad data lines.
- When you're working with binary, you'll almost always use hexadecimal representation in your program. You can learn to convert between binary and hex in your head. For example 2^17 is 20000 hex. (FYI - don't use ^ in your programs... It does NOT mean exponent in C++ !)
Though most, if not all, modern compilers should do the optimisation anyway.Quote:
Sometimes it's used a a quick way to multiply or divide by two (or powers of two).