1

I am reading a tutorial in objective - C but it uses some things of C / C++. I've this line of code:

 static const uint32_t projectileCategory     =  0x1 << 1;

So I want to know why does the variable equals 2 and if I can write it as

 static const uint32_t projectileCategory     =  0x2;

Thank you!

David Zech
  • 745
  • 3
  • 14
Baxter Lopez
  • 1,110
  • 1
  • 9
  • 14
  • it's written this way to get the code more readable. The coder wants the code reader to know that the interesting information in this line that the second bit of this variable is set to 1 and not that the value is 2. This is a common writing when we use masks later in the code... – hzrari Apr 08 '14 at 20:33
  • @hzrari I don't see where `0x1 << 1` is any more readable than `0x2`. Where the use of `<<` in constant exprssions becomes interesting is with things like `1 << someNamedConstant`. – James Kanze Apr 08 '14 at 20:57
  • @James Kanze: If the goal is to do some bits processing, 0x1 << 1 is more readable than 0x2. In other terms, code reader can see directly that the (i+1)th byte of (0x1 << i) is set to 1. If you want to test that the (i+1)th of an integer is set to 1 or to 0, you have just to test whether (integer & (1 << i)) is different from 0. If the goal is just to process integers as value, not as a serie of bits, of course 0x2 is more readable that 0x1 << 1 – hzrari Apr 08 '14 at 21:21
  • @hzrari How is `0x1 << 1` more readable than `0x2`? Both say exactly the same thing (at least to anyone who knows what hex means). – James Kanze Apr 08 '14 at 21:51
  • @James Kanze: What do you interpret if you read 0x0200? and what do you interpret if you read 1 << 9? For me, (0x0200) is a value like any other value, (1 << 9) says that the value doesn't matter, but what the coder want to show is that the bit 10 is set to 1. – hzrari Apr 08 '14 at 21:56

4 Answers4

4

This is called a left bit shift which is a bitwise operation, and is equivalent to your second example.

0x1 << 1 shifts the bits of value 0x1 1 to the left, which produces two.

0x1: 00000001
0x2: 00000010   <- product of 0x1 << 1
0x4: 00000100   <- product of 0x1 << 2 
Brandon
  • 16,382
  • 12
  • 55
  • 88
  • 1
    Except that in C++, `<<` is called the insertion operator, and is principally used to insert values in text format into an output stream. For historical reasons, it is also a bitwise left shift, but this meaning is clearly secondary and specialized. – James Kanze Apr 08 '14 at 21:03
3

They are the same, as others are saying. But sometimes if you have many constants which correspond to bits in a bitfield, you will see it written using bitshifts to clarify which bit it corresponds to. The spacing in the original code seems to indicate that it's part of a larger set of definitions. For example:

static const uint32_t projectileCategory     =  0x1 << 1;// bit 1
static const uint32_t doodadCategory         =  0x1 << 2;// bit 2
static const uint32_t playerCategory         =  0x1 << 3;// bit 3
static const uint32_t doorCategory           =  0x1 << 4;// bit 4

This is arguably clearer than setting them to 2, 4, 8, and 16.

tenfour
  • 36,141
  • 15
  • 83
  • 142
  • Except that you would just shift `1`, and not bother with the `0x1`, and that you'd never use a manifest constant as the shift count. (After all, there's no real difference in the readability between `0x04` and `1 << 2`.) – James Kanze Apr 08 '14 at 21:00
  • i agree with james, there's no real difference between the readability of `1<<15` and `0x8000` – Steve Cox Apr 08 '14 at 21:23
  • 1
    Sorry, but I think that if the intention of the code is to set the 16th bit to 1, 1 << 15 is more readable than 0x8000. By extension, (1 << 3) | (1 << 9) | (1 << 12) is more clearer than (0x0008) | (0x0200) | (0x1000) – hzrari Apr 08 '14 at 21:40
2

<< is Shift bits left

0x1 << 1 and 0x2 are same.i.e. 0x1(00000001) << 1 => 0x2(00000010)

The "static" part limits it's scope to that compilation unit. It also provides for static initialization. "const" just tells the compiler to not let anybody modify it. This variable is either put in the data or bss segment depending on the architecture, and might be in memory marked read-only.

Community
  • 1
  • 1
Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
0
(0x1 << 1)

and

0x02

are two expressions that evaluate to the same value with the same type.

ouah
  • 142,963
  • 15
  • 272
  • 331