0

I need to iterate through every bit of a byte. There are many ways to do it but my curiosity got piqued when trying to use a for loop with the "shift-left" operator as the iterator. This is what I would like to do.

for (byte i = 0x01; i <= 0x80; i << 1)
{
    value = value ^ i;
}

In this situation how could I use the shift-left operator (i << 1) for iteration? In a typical for loop, you would have (int i=0; i < 10; i++) the iterator being i++ for incrementing by one.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
JPRoy 1957
  • 21
  • 2

1 Answers1

2

You can use the compound assignment form of the operator like i <<= 1. Or you can simply use i = i << 1.

Emanuel Vintilă
  • 1,911
  • 4
  • 25
  • 35