Bit shifting words loses data

As a throw back to pic basic programming it's nice to see word as a datatype in Arduino 13. I'm finding it a little odd though.

For instance:

word myWord = word(B00000000, B11111000);

because it is word(HIGH, LOW) this gives me the equivalent of 1111100000000000 - that's fine, if not a little odd (why isn't it word(LOW, HIGH)?)

The trouble starts if I try to bit shift it... It loses the information...

myWord >> 1 produces 0111110000000000 myWord >> 2 produces 0011111000000000 etc. good until

myWord >> 4 produces 0000111100000000

I've dropped a bit and it keeps on going right up to

myWord >> 8 produces 0000000000000000

Any thoughts? Safer to use unsigned ints?


I think you're getting mixed up between big-endian and little-endian representations of a "word". If you swap the two halves of your word, then the shift is doing the right thing. Surely the PIC and the AVR store bytes in words the opposite way round?

Quite right. Thanks for the tip.