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!
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!
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.
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)
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
It could have been simply
enum
{
kFlag_FPS = 1,
kFlag_Help = 1 << 1,
kFlag_RedBlue3D = 1 << 2,
}
but the coder likes more symmetry.
>> (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
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.
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.