locked
how to check last bit of byte is one RRS feed

  • Question

  • i am having

    byte[] buffer;
    if((buffer[6])&(0x01))
    {

    }

    i want to check the last bit in the 6th byte should be one so i am applying this but it throws me error: Cannot implicitly convert type 'int' to 'bool'.


    m

    Thursday, September 13, 2012 4:24 PM

Answers

  • if (0 != (buffer[6] & 0x01))

    • Proposed as answer by desher Saturday, September 15, 2012 6:06 PM
    • Marked as answer by Lisa Zhu Thursday, September 20, 2012 10:07 AM
    Thursday, September 13, 2012 4:33 PM
  • Hi,

    Here is my code for this...I've create a "IsBitSet" method for checking a byte if a bit is set (1 = true) or not (0 = false). Play with the code and see if it fit your purpose

            public static bool IsBitSet(byte number, byte bit)
            {
                return ((number & bit) == bit) ;
            }
    
            static void Main(string[] args)
            {
                // 1 = 0000 0001
                // 2 = 0000 0010
                // 3 = 0000 0011
                // 4 = 0000 0100
                // 5 = 0000 0101
                // 6 = 0000 0110
                byte[] buffer = new byte[] { 0, 1, 2, 3, 5, 6 };
                byte byte6 = buffer[5]; // 6 = 0000 0110
    
                const byte bit0 = 0x1;
                const byte bit1 = 0x2;
                const byte bit2 = 0x4;
                const byte bit3 = 0x8;
    
                Console.WriteLine("Number: " + byte6);
                Console.WriteLine("result: Bit #0 = " + IsBitSet(byte6, bit0));
                Console.WriteLine("result: Bit #1 = " + IsBitSet(byte6, bit1));
                Console.WriteLine("result: Bit #2 = " + IsBitSet(byte6, bit2));
                Console.WriteLine("result: Bit #4 = " + IsBitSet(byte6, bit3));
            }
    


    Roar Jørstad Senior consultant http://www.evry.com

    • Proposed as answer by Lisa Zhu Monday, September 17, 2012 6:20 AM
    • Marked as answer by Lisa Zhu Thursday, September 20, 2012 10:08 AM
    Saturday, September 15, 2012 11:03 AM

All replies

  • if (0 != (buffer[6] & 0x01))

    • Proposed as answer by desher Saturday, September 15, 2012 6:06 PM
    • Marked as answer by Lisa Zhu Thursday, September 20, 2012 10:07 AM
    Thursday, September 13, 2012 4:33 PM
  • also note that 6th byte is buffer[5] (offset starts at 0). So the bool is like ((Buffer[5] & 1) != 0)
     
    /LM

    i am having

    byte[] buffer;
    if((buffer[6])&(0x01))
    {

    }

    i want to check the last bit in the 6th byte should be one so i am applying this but it throws me error: Cannot implicitly convert type 'int' to 'bool'.


    m


    /LM
    Friday, September 14, 2012 2:47 AM
  • Hi,

    Here is my code for this...I've create a "IsBitSet" method for checking a byte if a bit is set (1 = true) or not (0 = false). Play with the code and see if it fit your purpose

            public static bool IsBitSet(byte number, byte bit)
            {
                return ((number & bit) == bit) ;
            }
    
            static void Main(string[] args)
            {
                // 1 = 0000 0001
                // 2 = 0000 0010
                // 3 = 0000 0011
                // 4 = 0000 0100
                // 5 = 0000 0101
                // 6 = 0000 0110
                byte[] buffer = new byte[] { 0, 1, 2, 3, 5, 6 };
                byte byte6 = buffer[5]; // 6 = 0000 0110
    
                const byte bit0 = 0x1;
                const byte bit1 = 0x2;
                const byte bit2 = 0x4;
                const byte bit3 = 0x8;
    
                Console.WriteLine("Number: " + byte6);
                Console.WriteLine("result: Bit #0 = " + IsBitSet(byte6, bit0));
                Console.WriteLine("result: Bit #1 = " + IsBitSet(byte6, bit1));
                Console.WriteLine("result: Bit #2 = " + IsBitSet(byte6, bit2));
                Console.WriteLine("result: Bit #4 = " + IsBitSet(byte6, bit3));
            }
    


    Roar Jørstad Senior consultant http://www.evry.com

    • Proposed as answer by Lisa Zhu Monday, September 17, 2012 6:20 AM
    • Marked as answer by Lisa Zhu Thursday, September 20, 2012 10:08 AM
    Saturday, September 15, 2012 11:03 AM
  • That's a neat solution but you should remember to check for endedness.

    Digital Forensic Software Developer
    CCS LABS Digital Forensic Software
    Mark as Answer or Vote up if useful thank you!

    Saturday, September 15, 2012 11:10 AM
  • That's a neat solution but you should remember to check for endedness.


    Endedness? What do you mean?

    Roar Jørstad Senior consultant http://www.evry.com

    Saturday, September 15, 2012 11:43 AM