25

I have a C++ header file that contains the following definitions:

#define CACHE_NUM_WAYS    (1<<1)
#define CACHE_DATA_SIZE   (1<<8)

It is used as an integer in the rest of the code.

What does it mean? And what is its value?

plamut
  • 3,085
  • 10
  • 29
  • 40
itamar
  • 1,800
  • 4
  • 17
  • 30
  • Very close to [What does "<<" stand for in C#](http://stackoverflow.com/questions/2138359/what-does-stand-for-in-c?lq=1) – Bo Persson Jun 11 '12 at 15:31

8 Answers8

48

1 << 1 means:

00000000 00000001 changes to 00000000 00000010

1 << 8 means:

00000000 00000001 changes to 00000001 00000000

It's a bit shift operation. For every 1 on the right, you can think of yourself as multiplying the value on the left by 2. So, 2 << 1 = 4 and 2 << 2 = 8. This is much more efficient than doing 1 * 2.

Also, you can do 4 >> 1 = 2 (and 5 >> 1 = 2 since you round down) as the inverse operation.

John Humphreys
  • 37,047
  • 37
  • 155
  • 255
  • 3
    Practically, the efficiency point is not true, as the compilers are smart enough to convert `1<<1` into `1*2` or simply `2` if it sees the latter efficient. – Nawaz Jun 11 '12 at 15:36
  • 4
    @Nawaz: In most cases there is a variable like `i<<2` or `2< – joe Jun 14 '12 at 08:22
10

a<<b for integers means "shift left". The bitwise representation of a is shifted left b bits. This is the same as multiplying by (2 to the power of b).

So in your example, (1<<1) is 1*(2^1) is 2, (1<<8) is 1*(2^8) is 256.

It is worth pointing out that in general, as with other operators in c++, << may be overridden to perform other functions. By default, input/output streams override this operator to let you write concise code to send a bunch of parameters to the stream. So you may see code like this:

cout << something << somethingelse

and << does not mean left shift in this context.

moonshadow
  • 86,889
  • 7
  • 82
  • 122
6

<< is bitwise shift left (there is also >> bitwise shift right) if you have 32 bit integer

1      = 00000000 00000000 00000000 00000001 = 1
1 << 1 = 00000000 00000000 00000000 00000010 = 2
1 << 8 = 00000000 00000000 00000001 00000000 = 256
jackdoe
  • 1,846
  • 1
  • 15
  • 13
5

Those are bitwise shift operators.

http://msdn.microsoft.com/en-us/library/336xbhcz(v=vs.80).aspx

<< is shifting left so it is actually multiplying by 2 for << 1 and by 2^8 for << 8.

Ran
  • 4,117
  • 4
  • 44
  • 70
  • And that's why you have 2 for CACHE_NUM_WAYS, and 256 for CACHE_DATA_SIZE. – Marc Plano-Lesay Jun 11 '12 at 15:28
  • What is the difference between `#define CACHE_DATA_SIZE (1<<8)` and `#define CACHE_DATA_SIZE 256`. – Ash Burlaczenko Jun 11 '12 at 15:29
  • 2
    @AshBurlaczenko There is no difference. It just a different way to write it. `1<<8` highlights to you, that this is a single `1` at the eigth position, the rest is `0` (which is equal to 256) while `256` tells you the decimal value and does not directly tell you anything about set or cleared bits. – brimborium Jun 11 '12 at 15:32
  • @AshBurlaczenko They are the same value, I use (1<<8) when I want to bear in mind that the value is 2^N and that I am most likely looking at bit patterns and not just values. – NominSim Jun 11 '12 at 15:33
3

The operator << is a bitwise left-shift operator.

So when you write 1<<17, the binary representation of 1 is shifted left by 17 bits as:

//before (assume 1 is represented by 32-bit)
1 << 17  
0000 0000 0000 0000 0000 0000 0000 0001 << 17 (before - binary representation)

//after
0000 0000 0000 0010 0000 0000 0000 0000       (after - binary representation)
Nawaz
  • 353,942
  • 115
  • 666
  • 851
3

a<<b means (a * pow(2,b))-> a * 2^b //left-sift operator <<

a>>b means (a / pow(2,b))-> a / 2^b //right-sift operator >>

Rishi Raj
  • 79
  • 5
1

1<<8

here is how i understood it. in the most layman way, understand it as 1 (00000001) in binary number system is now promoted to the 8 places(bits) ahead from where it was earlier, and for every place as we know it moves the value(2 in binary) gets exponentially multiplied so it becomes 1*(2^8)=256.

so 1<<6 will be 1*(2^6)= 64 and so on

  • basically it's a bitwise shift operator. which shifts the bit by the specified places – Jain Nov 12 '21 at 06:50
1

In CPP, "<<" stands for the left-shift operator and ">>" stands for the right-shift operator.

x<<y is equivalent to multiplying x with 2^y and x>>y is equivalent to dividing x by 2^y.

In theoretical terms, the left shift operator shifts the bits of the first operand to the left and the operand on the right in this case decides the number of bits to shift to the left and similarly, the right shift operator shifts the bits of the first operand to the right and the operand on the right decides the number of bits to shift to the right.

Durbar_19
  • 11
  • 1