I'm fairly new to Arduino and I am currently working with a Serial GPS receiver.
I receive some returned bytes such as firmware versions as a byte, however the higher 2 bits form the major revision, then there should be a decimal place, then the lower 2 bits form the minor revision number.
Please can anyone help me or point me in the right direction to split bytes in 2? The bit() command only seems to allow you to look at individal bits, when I need a 'nibble', 2 bit size.
Im with Professor Chaos on this one, you could use some inline asm also such as
swapf, data
which swaps the high nib and low nib in a byte, it is useful when talking to 4bit lcd's
And I don't believe that there's anything in the C or C++ language
references that would guarantee that the 'high' bitfield would be aligned
with the four most significant bits nor the 'low' bitfield being aligned with
the four least significant bits. Maybe on the Arduino it might work....
For those concerned with performance... certain combinations of bit fields will generate several machine instructions. AlphaBeta's example is fairly lean. I think it's the same as using shift-and.
nothing that cannot be solved by a quick lookup table.
IIRC, the IP "Fragment offset" field can not be represented as a C bitfield on a little-endian CPU/compiler. It spans the byte boundry on a big-endian system, but ends up discontiguous on little-endian systems. So there are things that can't be solved with a lookup table...