Best way to convert a byte to 8 bit binary (for use as boolean flags)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    #1

    Best way to convert a byte to 8 bit binary (for use as boolean flags)

    I have an 8x8 grid containing squares that can be either black (off) or white (on). I'd like to use a single byte as the flags for each row, since a byte contains 8 bits. So for example, if I used the byte 154 for the first row, that translates to the binary 10011010, which means that squares 1, 4, 5, and 7 would be turned on, the others would be turned off.

    I've used methods of extracting boolean data from each bit of a byte, but they weren't very efficient (loops suck,) so I was hoping there'd be a better way.

    Does anyone have any advice that might make this simpler? Like I said, I plan to have each byte control one row of the 8x8 grid, so I might just use a 64 bit intger for the final number, since I'll need 64 flags anyway, easier than a byte array...maybe.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Not quite sure I understand what you're looking for... it's pretty easy to extract data from the byte with masking, but for checking all your flags I think looping is your only real choice. I guess you could also use an enum...

    Code:
    enum Flags
    {
      First = 0x01,
      Second = 0x02,
      Third = 0x04,
      Fourth = 0x08,
      ...
    }
    ...
    if ((theByte & (byte)Flags.Third) == (byte)Flags.Third)
    {
      // third flag is set
    }
    That only lets you check one at a time though, so I don't know if that helps... You could also loop through the enum values and apply them that way. Actually, I'm gonna stop rambling now, haha, I'm just making too many assumptions :D If that helps you, great, if not please post more details and we'll take another look.

    Comment

    • HaLo2FrEeEk
      Contributor
      • Feb 2007
      • 404

      #3
      How would I loop through an enum? I tried doing a simple:

      foreach(type value in enum)

      but it didn't work.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Check out Enum.GetValues. You can use that to iterate through all the values in an enum. You can use Enum.GetNames to go through all the enum names as strings too if you like.

        Comment

        • John McDonald
          New Member
          • Dec 2010
          • 1

          #5
          Bitwise operations are always a pain imo, I'd consider arrays, but here's some code that will loop from 0 to 7 and extract the value of the bit at the loop index from a single row:

          Code:
          byte row = 42;	// 00101010 in Binary
          for (byte i = 0; i < 8; i++)
          {
          	byte bit = (byte)(1 << i);
          	Console.Write(bit + "\t");
          	
          	bool isSet = (row & bit) > 0;
          	Console.WriteLine(isSet);
          }

          Comment

          Working...