Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 800 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C# binary literals

#1
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?
Reply

#2
While not possible using a Literal, maybe a [BitConverter][1] can also be a solution?


[1]:

[To see links please register here]

Reply

#3
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);
Reply

#4
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;
}
}
Reply

#5
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



Reply

#6
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);
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through