2
Answers

Bits in Enum flags

Photo of S

S

13y
1.5k
1

Hi

Just need assistance in clarifying the below please

I'm aware a bit has a single binary value of either 0 or 1 and 8 bits make a Byte etc etc. but how does this work in relation to Bits in an Enum flag.

For example:-

The values 0x0, 0x1, 0x2, 0x4 indicate powers of two. In computer bits, powers of two contain one bit set.

Quote "Where powers of two contain one bit set" does this mean it could be either a 0 or 1 in binary?

i.e. 0x0 is 0, 0x1 is 01, 0x2 is 11 and 0x4 is 1111?

Thanks

Answers (2)

0
Photo of Vulpes
NA 98.3k 1.5m 13y
0x0 is not in fact a power of two.

The first power of two is 0x1 which is 2 to the power 0.

If you think about it, for a binary number to be a power of two it needs to have exactly one bit set.

For example:

0x1 is 00000001
0x2 is 00000010
0x4 is 00000100
0x8 is 00001000

and so on.
Accepted
0
Photo of S
NA 381 98.2k 13y
Understood fully thanks kindly Vulpes!