234

Is there a way to write binary literals in C#, like prefixing hexadecimal with 0x? 0b doesn't work.

If not, what is an easy way to do it? Some kind of string conversion?

3
  • 2
    Use constants and give them proper names, then the fact that you have to write them out in decimal shouldn't matter. Personally I'd want to read if (a == MaskForModemIOEnabled) rather than if (a == 0b100101) Jul 29, 2010 at 11:44
  • 2
    Another note: If you found this post because you want a bit field, please consider the flags-attribute: msdn.microsoft.com/en-us/library/cc138362.aspx#sectionToggle0
    – toxvaerd
    Nov 9, 2010 at 10:29
  • 20
    Both very good advice, except when defining said bitfields, where these named constant definitions can be easier to read and write with a binary literal. [Flags]enum Quantity { None=0, One=1, Some=0b10, Most=0b100, All=0b111 }
    – brianary
    Dec 28, 2010 at 23:43

12 Answers 12

232

Update

C# 7.0 now has binary literals, which is awesome.

[Flags]
enum Days
{
    None = 0,
    Sunday    = 0b0000001,
    Monday    = 0b0000010,   // 2
    Tuesday   = 0b0000100,   // 4
    Wednesday = 0b0001000,   // 8
    Thursday  = 0b0010000,   // 16
    Friday    = 0b0100000,   // etc.
    Saturday  = 0b1000000,
    Weekend = Saturday | Sunday,
    Weekdays = Monday | Tuesday | Wednesday | Thursday | Friday
}

Original Post

Since the topic seems to have turned to declaring bit-based flag values in enums, I thought it would be worth pointing out a handy trick for this sort of thing. The left-shift operator (<<) will allow you to push a bit to a specific binary position. Combine that with the ability to declare enum values in terms of other values in the same class, and you have a very easy-to-read declarative syntax for bit flag enums.

[Flags]
enum Days
{
    None        = 0,
    Sunday      = 1,
    Monday      = 1 << 1,   // 2
    Tuesday     = 1 << 2,   // 4
    Wednesday   = 1 << 3,   // 8
    Thursday    = 1 << 4,   // 16
    Friday      = 1 << 5,   // etc.
    Saturday    = 1 << 6,
    Weekend     = Saturday | Sunday,
    Weekdays    = Monday | Tuesday | Wednesday | Thursday | Friday
}
13
  • 27
    @ColeJohnson: Many developers prefer that. I'm not as good at converting hex in my head as some developers are, and sometimes it's best to cater to the lowest common denominator. Jun 22, 2012 at 2:50
  • 2
    I think this is the most effective way since enums are built as constants. With the optional [Flags] attribute they can be used in bitwise operations (not directly related to the question, but particularly useful when describing binary literals). Another interesting possibility is to force the enum type as a built-in type (in this example, add ' : byte' after 'Days'); see built-in types in bit.ly/MKv42E
    – caligari
    Jul 7, 2012 at 18:37
  • 6
    @MikeT: Can you elaborate (or provide a link that elaborates) on that last thought? I often declare enums that start with "1" because I want to be able to detect and fail-fast when a value was simply never initialized. But there are some situations where a default value actually makes sense, and I'd think there wouldn't be anything wrong with adding a name for that value. Jun 10, 2015 at 15:24
  • 5
    @MikeT From ca1008: "If an enumeration that has the FlagsAttribute applied defines a zero-valued member, its name should be 'None' to indicate that no values have been set in the enumeration." So it's perfectly valid to add it to serve as default value, if it's called "None". Overall it just seems cleaner to me if the value in a default-initialised field actually has a name to show.
    – Nyerguds
    Jan 9, 2019 at 11:06
  • 1
    @JohnDemetriou. There is no difference between those in the compiled code, for the same reason 01 is no different from 1. But including those zeros can help developers to visualize where the ones and zeros are. May 17, 2022 at 16:04
183

C# 7.0 supports binary literals (and optional digit separators via underscore characters).

An example:

int myValue = 0b0010_0110_0000_0011;

You can also find more information on the Roslyn GitHub page.

3
  • 2
    It helps to remember that C#'s Binary Literals are big-endian, but on x86 integers are little-endian, so Int16 = 0b0010_0110_0000_0011 will be stored as { 0b0000_0011, 0b0010_0110 } - confusing.
    – Dai
    May 20, 2019 at 13:36
  • 3
    @Dai that’s no different than hex literals. In other words, all constants are big endian, but stored little endian on Intel/AMD and most ARMs. 0xDEADBEEF will be stored as 0xEF 0xBE 0xAD 0xDE
    – Cole Tobin
    Sep 2, 2019 at 18:28
  • This is because human language (at least western?) is big endian and number literals in source code match that. Thankfully. May 27 at 9:44
118

Only integer and hex directly, I'm afraid (ECMA 334v4):

9.4.4.2 Integer literals Integer literals are used to write values of types int, uint, long, and ulong. Integer literals have two possible forms: decimal and hexadecimal.

To parse, you can use:

int i = Convert.ToInt32("01101101", 2);
0
28

Adding to @StriplingWarrior's answer about bit flags in enums, there's an easy convention you can use in hexadecimal for counting upwards through the bit shifts. Use the sequence 1-2-4-8, move one column to the left, and repeat.

[Flags]
enum Scenery
{
  Trees   = 0x001, // 000000000001
  Grass   = 0x002, // 000000000010
  Flowers = 0x004, // 000000000100
  Cactus  = 0x008, // 000000001000
  Birds   = 0x010, // 000000010000
  Bushes  = 0x020, // 000000100000
  Shrubs  = 0x040, // 000001000000
  Trails  = 0x080, // 000010000000
  Ferns   = 0x100, // 000100000000
  Rocks   = 0x200, // 001000000000
  Animals = 0x400, // 010000000000
  Moss    = 0x800, // 100000000000
}

Scan down starting with the right column and notice the pattern 1-2-4-8 (shift) 1-2-4-8 (shift) ...


To answer the original question, I second @Sahuagin's suggestion to use hexadecimal literals. If you're working with binary numbers often enough for this to be a concern, it's worth your while to get the hang of hexadecimal.

If you need to see binary numbers in source code, I suggest adding comments with binary literals like I have above.

1
  • You can make this even easier like this: [Flags] public enum Scenery { Trees = 0x001, Grass = 1 << 1, Flowers = 1 << 2, Cactus = 1 << 3, }; You just start with 0 or 1, then each one following 1 (starting with 2) simply shifts left by an increasing operand. << 1, << 2, << 3, << 4, << 5, and so on. Easier to do correctly and edit when a big enum needs to be changed. (EDIT: If someone can help format that better, please do) Oct 26, 2022 at 4:31
25

You can always create quasi-literals, constants which contain the value you are after:

const int b001 = 1;
const int b010 = 2;
const int b011 = 3;
// etc ...
Debug.Assert((b001 | b010) == b011);

If you use them often then you can wrap them in a static class for re-use.

However, slightliy off-topic, if you have any semantics associated with the bits (known at compile time) I would suggest using an Enum instead:

enum Flags
{ 
    First = 0,
    Second = 1,
    Third = 2,
    SecondAndThird = 3
}
// later ...
Debug.Assert((Flags.Second | Flags.Third) == Flags.SecondAndThird);
18

If you look at the language feature implementation status of the .NET Compiler Platform ("Roslyn") you can clearly see that in C# 6.0 this is a planned feature, so in the next release we can do it in the usual way.

Binary literal status

0
3
string sTable="static class BinaryTable\r\n{";
string stemp = "";
for (int i = 0; i < 256; i++)
{
stemp = System.Convert.ToString(i, 2);
while(stemp.Length<8) stemp = "0" + stemp;
sTable += "\tconst char nb" + stemp + "=" + i.ToString() + ";\r\n";
}
sTable += "}";
Clipboard.Clear();
Clipboard.SetText ( sTable);
MessageBox.Show(sTable);

Using this, for 8bit binary, I use this to make a static class and it puts it into the clipboard.. Then it gets pasted into the project and added to the Using section, so anything with nb001010 is taken out of a table, at least static, but still... I use C# for a lot of PIC graphics coding and use 0b101010 a lot in Hi-Tech C

--sample from code outpt--

static class BinaryTable
{   const char nb00000000=0;
    const char nb00000001=1;
    const char nb00000010=2;
    const char nb00000011=3;
    const char nb00000100=4;
//etc, etc, etc, etc, etc, etc, etc, 
}

:-) NEAL

3

Binary literal feature was not implemented in C# 6.0 & Visual Studio 2015. but on 30-March 2016 Microsoft announced the new version of Visual Studio '15' Preview with that we can use binary literals.

We can use one or more than one Underscore( _ ) character for digit separators. so the code snippet would look something like:

int x           = 0b10___10_0__________________00; //binary value of 80
int SeventyFive = 0B100_________1011; //binary value of 75

WriteLine($" {x} \n {SeventyFive}");

and we can use either of 0b and 0B as shown in the above code snippet.

if you do not want to use digit separator you can use it without digit separator like below code snippet

int x           = 0b1010000; //binary value of 80
int SeventyFive = 0B1001011; //binary value of 75

WriteLine($" {x} \n {SeventyFive}");
2

While not possible using a Literal, maybe a BitConverter can also be a solution?

3
  • BitConverter gets a bit tricky because it is machine-endian; and on your standard Intel, that means little-endian. Most people have difficulty reading little-endian literals... Feb 27, 2009 at 13:33
  • 1
    Ouch, now i remember why I was always aware of but never used the BitConverter... Feb 27, 2009 at 15:07
  • 4
    BitConverter has the field BitConverter.IsLittleEndian, which you can use to test (and reverse a buffer) if the host machine not little endian.
    – foxy
    Jul 19, 2011 at 21:12
1

Though the string parsing solution is the most popular, I don't like it, because parsing string can be a great performance hit in some situations.

When there is needed a kind of a bitfield or binary mask, I'd rather write it like

long bitMask = 1011001;

And later

int bit5 = BitField.GetBit(bitMask, 5);

Or

bool flag5 = BitField.GetFlag(bitMask, 5);`

Where BitField class is

public static class BitField
{
    public static int GetBit(int bitField, int index)
    {
        return (bitField / (int)Math.Pow(10, index)) % 10;
    }

    public static bool GetFlag(int bitField, int index)
    {
        return GetBit(bitField, index) == 1;
    }
}
1
  • 4
    Yuck. Why not just use enums? Using ont thousand to mean 1-0-0-0 is just asking for trouble.
    – Clément
    Feb 26, 2012 at 22:33
1

Basically, I think the answer is NO, there is no easy way. Use decimal or hexadecimal constants - they are simple and clear. @RoyTinkers answer is also good - use a comment.

int someHexFlag = 0x010; // 000000010000
int someDecFlag = 8;     // 000000001000

The others answers here present several useful work-a rounds, but I think they aren't better then the simple answer. C# language designers probably considered a '0b' prefix unnecessary. HEX is easy to convert to binary, and most programmers are going to have to know the DEC equivalents of 0-8 anyways.

Also, when examining values in the debugger, they will be displayed has HEX or DEC.

1

You can use 0b000001 since Visual Studio 2017 (C# 7.0)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.