Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would someone use the << operator in an enum declaration?

I was looking at the code I have currently in my project and found something like this:

public enum MyEnum
{
    open     = 1 << 00,
    close    = 1 << 01,
    Maybe    = 1 << 02,
    ........
}

The << operand is the shift operand, which shifts the first operand left by the number bits specified in the second operand.

But why would someone use this in an enum declaration?

like image 851
JSBach Avatar asked Jul 26 '13 18:07

JSBach


People also ask

What is the point of enums?

The entire point of enums are to make your code more readable. They are not casted to ints, since they are by default ints. The only difference is instead of going back and forth checking what int values you may have, you can have a clear set of enums to make the code more readable.

Which of the following datatype can be used with enum?

The enum can be of any numeric data type such as byte, sbyte, short, ushort, int, uint, long, or ulong. However, an enum cannot be a string type.

What is enum class in C#?

An enum is a special "class" that represents a group of constants (unchangeable/read-only variables).


6 Answers

This allows you to do something like this:

var myEnumValue = MyEnum.open | MyEnum.close;

without needing to count bit values of multiples of 2.

(like this):

public enum MyEnum
{
    open     = 1,
    close    = 2,
    Maybe    = 4,
    ........
}
like image 133
Mark Avenius Avatar answered Oct 05 '22 15:10

Mark Avenius


This is usually used with bitfields, since it's clear what the pattern is, removes the need to manually calculate the correct values and hence reduces the chance of errors

[Flags]
public enum SomeBitField
{
    open = 1 << 0    //1
    closed = 1 << 1  //2
    maybe = 1 << 2   //4
    other = 1 << 3   //8
    ...
}
like image 38
Lee Avatar answered Oct 05 '22 13:10

Lee


To avoid typing out the values for a Flags enum by hand.

public enum MyEnum
{
    open     = 0x01,
    close    = 0x02,
    Maybe    = 0x04,
    ........
}
like image 24
Richard Deeming Avatar answered Oct 05 '22 13:10

Richard Deeming


This is to make an enum that you can combine.

What it effectively means is this:

public enum MyEnum
{
    open = 1;
    close = 2;
    Maybe = 4;
    //...
}

This is just a more bulletproof method of creating a [Flags] enum.

like image 45
It'sNotALie. Avatar answered Oct 05 '22 15:10

It'sNotALie.


It's just meant to be a cleaner / more intuitive way of writing the bits. 1, 2, 3 is a more human-readable sequence than 0x1, 0x2, 0x4, etc.

like image 34
McGarnagle Avatar answered Oct 05 '22 13:10

McGarnagle


Lots of answers here describing what this mechanic allows you to do, but not why you would want to use it. Here's why.

Short version:

This notation helps when interacting with other components and communicating with other engineers because it tells you explicitly what bit in a word is being set or clear instead of obscuring that information inside a numeric value.

So I could call you up on the phone and say "Hey, what bit is for opening the file?" And you'd say, "Bit 0". And I'd write in my code open = 1 << 0. Because the number to the right of << tells you the bit number.

.

Long version:

Traditionally bits in a word are numbered from right to left, starting at zero. So the least-significant bit is bit number 0 and you count up as you go toward the most-significant bit. There are several benefits to labeling bits this way.

One benefit is that you can talk about the same bit regardless of word size. E.g., I could say that in both the 32-bit word 0x384A and 8-bit word 0x63, bits 6 and 1 are set. If you numbered your bits in the other direction, you couldn't do that.

Another benefit is that a bit's value is simply 2 raised to the power of the bit position. E.g., binary 0101 has bits 2 and 0 set. Bit 2 contributes the value 4 (2^2) to the number, and bit 0 contributes the value 1 (2^0). So the number's value is of course 4 + 1 = 5.

That long-winded background explanation brings us to the point: The << notation tells you the bit number just by looking at it.

The number 1 by itself in the statement 1 << n is simply a single bit set in bit position 0. When you shift that number left, you're then moving that set bit to a different position in the number. Conveniently, the amount you shift tells you the bit number that will be set.

1 << 5:   This means bit 5.   The value is 0x20.
1 << 12:  This means bit 12.  The value is 0x40000.
1 << 17:  This means bit 17.  The value is 0x1000000.
1 << 54:  This means bit 54.  The value is 0x40000000000000.
          (You can probably see that this notation might be helpful if
          you're defining bits in a 64-bit number)

This notation really comes in handy when you're interacting with another component, like mapping bits in a word to a hardware register. Like you might have a device that turns on when you write to bit 7. So the hardware engineer would write a data sheet that says bit 7 enables the device. And you'd write in your code ENABLE = 1 << 7. Easy as that.

Oh shoot. The engineer just sent an errata to the datasheet saying that it was supposed to be bit 15, not bit 7. That's OK, just change the code to ENABLE = 1 << 15.

What if ENABLE were actually when both bits 7 and 1 were set at the same time?

ENABLE = (1 << 7) | (1 << 1).

It might look weird and obtuse at first, but you'll get used to it. And you'll appreciate it if you ever explicitly need to know the bit number of something.

like image 32
indiv Avatar answered Oct 05 '22 13:10

indiv