bitwise shift operators, where does the bit go?

el_supremo:

for(byte i=0;i<8;i++)

{   data >>=1;
   if(overflow) {
       do blah;
   }
}



A right shift cannot create an overflow condition. All you need to do is test the low order bit and shift the data *afterwards*.


for(byte i=0;i<8;i++)
{
   if(data & 1) {
       do blah;
   }
  data >>= 1;
}




If you need to test the high order bit first this code will work:


for(byte i=0;i<8;i++)
{
   if(data & 0x80) {
       do blah;
   }
  data <<= 1;
}



If data is a signed byte you can use (data < 0) instead of (data & 0x80).

Pete

How are either of those examples any different in efficiency than the first example I gave? Whether you shift the data or the comparator, the efficiency is the same. I am trying to eek a bit more time out by making use of the underlying hardware withing the processor. In assembler you to a test under mask and then branch on a condition. I was just thinking that the shift and branch on condition would be faster but perhaps I am mistaken.