none
Determining the value of a specific bit in a byte RRS feed

  • Question

  • I have 8 lights (0-7) and I am receiving a byte where each bit represents the on/off status of a light.

    Let's say I want to know the status of light #3.

    What's the "formula" to get this from the byte?

    Thanks!

    Friday, April 26, 2019 8:57 PM

Answers

  • Do you just want to know on or off, or do you actually need 1 or 0?

        int bit3 = light & (1 << 3);

    That will result 0 or 8.  If you really need 1 or 0, you can use:

        int bit3 = (light >> 3) & 1;


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by T Gregory Monday, April 29, 2019 5:33 PM
    Saturday, April 27, 2019 12:51 AM

All replies

  • Do you just want to know on or off, or do you actually need 1 or 0?

        int bit3 = light & (1 << 3);

    That will result 0 or 8.  If you really need 1 or 0, you can use:

        int bit3 = (light >> 3) & 1;


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by T Gregory Monday, April 29, 2019 5:33 PM
    Saturday, April 27, 2019 12:51 AM
  • I have 8 lights (0-7) and I am receiving a byte where each bit represents the on/off status of a light.

    Let's say I want to know the status of light #3.

    What's the "formula" to get this from the byte?

    For some alternatives and accompanying explanations see this thread:

    Get a specific bit from byte
    https://stackoverflow.com/questions/4854207/get-a-specific-bit-from-byte

    - Wayne

    Saturday, April 27, 2019 2:13 AM
  • I have 8 lights (0-7) and I am receiving a byte where each bit represents the on/off status of a light.

    Let's say I want to know the status of light #3.

    What's the "formula" to get this from the byte?


    The later versions of C# support binary literals. So one can specify masks
    which are more easily read than bit shifts, etc. Here's an example. Note
    that I have numbered the bit positions 0-7 from left to right. One could
    of course number/name them 7-0 or 1-8 or 8-1 according to preference, 
    context or accepted convention.

    class bits_test
    {
        public const byte bit0 = 0b10000000;
        public const byte bit1 = 0b01000000;
        public const byte bit2 = 0b00100000;
        public const byte bit3 = 0b00010000;
        public const byte bit4 = 0b00001000;
        public const byte bit5 = 0b00000100;
        public const byte bit6 = 0b00000010;
        public const byte bit7 = 0b00000001;
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            byte B = 0x99;
    
            if((B & bits_test.bit0) == bits_test.bit0)
            {
                Console.WriteLine("bit 0 is set");
            }
    
            if ((B & bits_test.bit1) == bits_test.bit1)
            {
                Console.WriteLine("bit 1 is set");
            }
            
            if ((B & bits_test.bit3) == bits_test.bit3)
            {
                Console.WriteLine("bit 3 is set");
            }
    
            if ((B & bits_test.bit7) == bits_test.bit7)
            {
                Console.WriteLine("bit 7 is set");
            }
        }
    }
    
    /*
    bit 0 is set
    bit 3 is set
    bit 7 is set
    */
    
    

    - Wayne

    Saturday, April 27, 2019 3:14 AM
  • class bits_test
    {
        public const byte bit0 = 0b10000000;
        public const byte bit1 = 0b01000000;
        public const byte bit2 = 0b00100000;
        public const byte bit3 = 0b00010000;
        public const byte bit4 = 0b00001000;
        public const byte bit5 = 0b00000100;
        public const byte bit6 = 0b00000010;
        public const byte bit7 = 0b00000001;
    }

    For God's sake, don't do that!  Future programmers will curse your name.  In virtually every hardware environment in the world, bits are numbered from right to left, so the low-order bit is bit 0.  You have done it backwards.  Instead, do this:

    class bits_test
    {
        public const byte bit7 = 0b10000000;
        public const byte bit6 = 0b01000000;
        public const byte bit5 = 0b00100000;
        public const byte bit4 = 0b00010000;
        public const byte bit3 = 0b00001000;
        public const byte bit2 = 0b00000100;
        public const byte bit1 = 0b00000010;
        public const byte bit0 = 0b00000001;
    }

    Although I have to say I'm not convinced that (B & bits_test.bit7) is any easier to read than (B & (1<<7)).


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Saturday, April 27, 2019 7:28 AM
  •  You have done it backwards.  

    Did you not read my comment?

    "One could of course number/name them 7-0"

    >In virtually every hardware environment in the world, bits are numbered from
    >right to left, so the low-order bit is bit 0.

    You're familiar with the inner workings and technical details of every computer
    system in the world? An amazing boast, and one that I find hard to accept. In my
    own experience I have found that both numbering patterns exist - or have existed.

    I recall being somewhat miffed when providing technical support on Univac/Unisys
    mainframes and discovering that the numbering order in the manuals was reversed
    on newer models of the 2200 line compared to what I was familiar with on the
    earlier 1100 line. This in spite of the fact that they were "compatible" members
    of the same family.

    The line had/has 36-bit words, and the instruction word format was organized
    as shown here:

    Note the numbering of the bits from 0-35, left to right.

    >I have to say I'm not convinced that (B & bits_test.bit7) is any easier to
    >read than (B & (1<<7)).

    Probably not for someone who is very familiar with bit-shifting operations.
    In my experience many programmers aren't, but will find the explicit display
    of the bits in a binary literal easier to recognize and work with than trying
    to visualize what is happening to the bits during a shift. Even more so if
    multiple shifts are used.

    Using binary literals for better clarity over bit shifts becomes even more
    evident when types larger than 8 bits are involved. IMO, YMMV.

    - Wayne

    Saturday, April 27, 2019 8:39 AM
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 29, 2019 2:33 AM
    Moderator
  • Hola!! Buenos dias, si te hiciera falta determinar por ejemplo la posicion 3 del byte que tienes, lo que tendrias q hacer es un ''and'' entre ese byte y un byte con la posicion que deseas saber en uno, en este caso seria 00001000 pq deseas encontrar el valor de la posicion 3, luego si el resultado da 0, el valor del bit 3 es 0, si da distinto de cero el valor del bit 3 es 1.
    Monday, April 29, 2019 2:27 PM