55
 enum
    {
      kFlag_FPS         = 1 << 0,
      kFlag_Help        = 1 << 1,
      kFlag_RedBlue3D   = 1 << 2,
    }

I am trying to understand what this code is I don't quite know what:

1 << 0

means?

Any help is greatly appreciated!

CodeDoctorJL
  • 1,216
  • 4
  • 16
  • 29
  • 28
    It's just to make it look consistent. – Mysticial Aug 13 '13 at 17:39
  • It is bitwise shifting to the left but I don't know how to explain that. – putvande Aug 13 '13 at 17:40
  • 5
    1 << 0 is 1 shifted left 0 times, or 1 * 2^0. So, it's 1. – BrainSteel Aug 13 '13 at 17:40
  • @Mysticial "Consistent" means format, the code looks good. The idea is just to assign a number into variables. Here they use bits number system to assign a simple variable :D by shifting 1 to the left in binary. i,e var =1; same as var = 0000 0001 and var=2 is var=0000 0010. C language is crazy :D – Dhiren Hamal Mar 02 '22 at 08:32

8 Answers8

79

From MSDN - Shift Operators: >> and <<

The left-shift operator causes the bit pattern in the first operand to be shifted to the left by the number of bits specified by the second operand. Bits vacated by the shift operation are zero-filled. This is a logical shift instead of a shift-and-rotate operation.

This means that the user is taking the bits value of 1 and shifting the bits to the left based on the right number.

That means that in this case, their values will look like this in binary.

1 << 0 = `0000 0001`
1 << 1 = `0000 0010`
1 << 2 = `0000 0100`

The first shift is not necessary, but it looks more consistent with the rest.

Caesar
  • 9,483
  • 8
  • 40
  • 66
21

1 << 0 is 1 shifted to the left by 0 positions, which is just 1.

1''
  • 26,823
  • 32
  • 143
  • 200
9

x << y - means shift bits of x to the left (to larger value) y times. In math, this looks like: x * (2^y) or x * pow(2, y)

Victor.Palyvoda
  • 523
  • 3
  • 12
5

The << operator is a bit shifter. So 1 << 2, is equal to 4 as you take 1 and shift by 2 bits. When using 1 << 0, that has no impact on the value and is probably there to make everything appear consistent

sedavidw
  • 11,116
  • 13
  • 61
  • 95
4

It could have been simply

enum
    {
      kFlag_FPS         = 1,
      kFlag_Help        = 1 << 1,
      kFlag_RedBlue3D   = 1 << 2,
    }

but the coder likes more symmetry.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

>> (Signed right shift) if the number is negative, then 1 is used as a filler and if the number is positive, then 0 is used as a filler.

       int x = -4; 
       System.out.println(x>>1);  //output -2  
       int y = 4; 
       System.out.println(y>>1); //output 2

>>> (Unsigned right shift) In Java, the operator ‘>>>’ is unsigned right shift operator. It always fills 0 irrespective of the sign of the number. // x is stored using 32 bit 2's complement form.
// Binary representation of -1 is all 1s (111..1)

       int x = -1;   
       System.out.println(x>>>29);  // The value of 'x>>>29' is 00...0111 
       System.out.println(x>>>30);  // The value of 'x>>>30' is 00...0011  
       System.out.println(x>>>31);  // The value of 'x>>>31' is 00...0001
Ping Woo
  • 1,423
  • 15
  • 21
0

It's a redundant operation made to follow a pattern (I'd argue) arbitrarily.

tl;dr it's just 1

1 as int 32 is:

00000000 000000000 00000000 00000001

1 << 1 shifts everything over to the left once, yielding 2

00000000 000000000 00000000 00000010

So, 1 << 0 shifts everything to the left zero times, yielding an unchanged 1

00000000 000000000 00000000 00000001

For those using this in an enum:

enum {
   firstOption = 0,
   secondOption = (1 << 0),
   thirdOption = (1 << 1),
   // etc.
}

To avoid having to reference this page, if you really want to maintain a pattern (for whatever reason), it's probably best to do:

enum {
   firstOption = (0 << 0),
   secondOption = (1 << 0),
   thirdOption = (1 << 1),
   // etc.
}

But that seems a little silly to me. I'd just prefer:

enum {
   firstOption = 0,
   secondOption = 1, // was that so hard?
   thirdOption = (1 << 1),
   // etc.
}

It's ironic that 1 << 0 is meant to clarify, yet here we all are.

h.and.h
  • 690
  • 1
  • 8
  • 26
0

I was searching for the answer to this question myself, but I was more curious about the why. Here is a link that properly explains the why. Why not just use standard enums that takes integers as their values? Apparently, using bitwise operators for enums are great for defining flags.

// Powers of two
[Flags] public enum AttackType {
    // Decimal     // Binary
    None   = 0,    // 000000
    Melee  = 1,    // 000001
    Fire   = 2,    // 000010
    Ice    = 4,    // 000100
    Poison = 8     // 001000
}
// Label          Binary   Decimal
// Melee:         000001 = 1
// Fire:          000010 = 2
// Melee | Fire:  000011 = 3

With the above you can combine multiple flags/enums and have a unique value without having to define it in the actual enum class. Use this link to find out more.

Grace Em
  • 21
  • 3