I don't under stand what the << does as in 2<<16. If you could explain it I'd be vary thankful

This is a discussion on *2<<16* within the **C++ Programming** forums, part of the General Programming Boards category; I don't under stand what the << does as in 2<<16. If you could explain it I'd be vary thankful...

- 08-09-2005 #1
## 2<<16

I don't under stand what the << does as in 2<<16. If you could explain it I'd be vary thankful

- 08-09-2005 #2
That's a bitwise left shift, and basically your expression can be seen as:

2 * 2**16, where ** denotes exponentiation.Originally Posted by**Bjarne Stroustrup (2000-10-14)**

- 08-09-2005 #3

- Join Date
- Apr 2003
- Posts
- 2,663

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:

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:

Code:16's 8's 4's 2's 1's

Code:1 8's 4's 2's 1's

Code:1 0 8's 4's 2's 1's

Code:1 0 1 8's 4's 2's 1's

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,072Last edited by 7stud; 08-09-2005 at 11:40 PM.

- 08-10-2005 #4

- Join Date
- Sep 2001
- Posts
- 1,398

**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++ !)

- 08-10-2005 #5Sometimes it's used a a quick way to multiply or divide by two (or powers of two).Originally Posted by
**Bjarne Stroustrup (2000-10-14)**

- Exactly how to get started with C++ (or C) today
- C Tutorial
- C++ Tutorial
- 5 ways you can learn to program faster
- The 5 Most Common Problems New Programmers Face
- How to set up a compiler
- 8 Common programming Mistakes
- What is C++11?
- Creating a game, from start to finish

- How to create a shared library on Linux with GCC - December 30, 2011
- Enum classes and nullptr in C++11 - November 27, 2011
- Learn about The Hash Table - November 20, 2011
- Rvalue References and Move Semantics in C++11 - November 13, 2011
- C and C++ for Java Programmers - November 5, 2011
- A Gentle Introduction to C++ IO Streams - October 10, 2011