开发者

Can I embed a number in binary instead of hexadecimal?

How can I write binary numb开发者_运维问答er in code without using

Convert.ToInt16("00001000", 2);

I want it to be very fast, I don't want to read number from string. Is there any faster way?


From the C# Language Specification:

2.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.

You can do it your way:

Convert.ToInt16("00001000", 2);

Or representing the number in hex format for example using the 0x prefix, as in:

0x8

I am not aware of any other way.


You can write the number in hexadecimal: 0x8.
Google Calculator can convert between bases.


Usually, for the sake of avoiding any silly errors, when I define a "flags" enum I'll use the bit-shift operator to specify the values and just let the compiler figure it out. For example:

[Flags]
enum FlagsEnum
{
    Zero = 0,
    One = 1 << 0,
    Two = 1 << 1,
    Four = 1 << 2,
    Eight = 1 << 3,
    Sixteen = 1 << 4,
    ThirtyTwo = 1 << 5,
    SixtyFour = 1 << 6,
    OneHundredTwentyEight = 1 << 7,
    // etc.
}

I find this style easier to maintain when I inevitably reorder and add/remove values from my enum.

So in your case, you could specify your binary value "00001000" as (1 << 3). If you had something more complicated, though, like "00101000", then this wouldn't work too well; you could do something like (1 << 5) | (1 << 3), but for that case I would just specify the value in hex.


I've got a special class for this

public static class B
{
    public static readonly V _0000 = 0x0;
    public static readonly V _0001 = 0x1;
    public static readonly V _0010 = 0x2;
    public static readonly V _0011 = 0x3;
    public static readonly V _0100 = 0x4;
    public static readonly V _0101 = 0x5;
    public static readonly V _0110 = 0x6;
    public static readonly V _0111 = 0x7;

    public static readonly V _1000 = 0x8;
    public static readonly V _1001 = 0x9;
    public static readonly V _1010 = 0xA;
    public static readonly V _1011 = 0xB;
    public static readonly V _1100 = 0xC;
    public static readonly V _1101 = 0xD;
    public static readonly V _1110 = 0xE;
    public static readonly V _1111 = 0xF;

    public struct V
    {
        ulong Value;

        public V(ulong value)
        {
            this.Value = value;
        }

        private V Shift(ulong value)
        {
            return new V((this.Value << 4) + value);
        }

        public V _0000 { get { return this.Shift(0x0); } }
        public V _0001 { get { return this.Shift(0x1); } }
        public V _0010 { get { return this.Shift(0x2); } }
        public V _0011 { get { return this.Shift(0x3); } }
        public V _0100 { get { return this.Shift(0x4); } }
        public V _0101 { get { return this.Shift(0x5); } }
        public V _0110 { get { return this.Shift(0x6); } }
        public V _0111 { get { return this.Shift(0x7); } }

        public V _1000 { get { return this.Shift(0x8); } }
        public V _1001 { get { return this.Shift(0x9); } }
        public V _1010 { get { return this.Shift(0xA); } }
        public V _1011 { get { return this.Shift(0xB); } }
        public V _1100 { get { return this.Shift(0xC); } }
        public V _1101 { get { return this.Shift(0xD); } }
        public V _1110 { get { return this.Shift(0xE); } }
        public V _1111 { get { return this.Shift(0xF); } }

        static public implicit operator V(ulong value)
        {
            return new V(value);
        }

        static public implicit operator ulong(V this_)
        {
            return this_.Value;
        }

        static public implicit operator uint(V this_)
        {
            return (uint)this_.Value;
        }

        static public implicit operator ushort(V this_)
        {
            return (ushort)this_.Value;
        }

        static public implicit operator byte(V this_)
        {
            return (byte)this_.Value;
        }
    }
}

Examples:

byte x = B._0010._0011;
ushort y = B._1001._0011._1111._0000;
uint z = B._1001._0011._1111._1000._0011;
ulong d = B._1001._0011._1111._1000._0011._0001;

Implemented in CityLizard Framework (CityLizard.Binary.dll).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜