PDA

View Full Version : -SOLVED - Bytes problem



grasshopper
03-20-2009, 10:58 PM
If I have 8 bytes and all I am interested in is the last bit of each how would I put that all the bits in another byte. For example


Var
Byte X1,X2,X3,X4,X5,X6,X7,X8 ,Total

X1 := %0001
x2 := %0000
x3 := %0001
X4 := %0001
x5 := %0000
x6 := %0001
X7 := %0001
x8 := %0001

Total would equal %10110111

Could I bit shift and OR them some how?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Visit my site -> www.rawcircuits.com (Http://www.rawcircuits.com)

Post Edited (grasshopper) : 3/20/2009 4:24:07 PM GMT

Joćo Geada
03-21-2009, 03:19 AM
Grasshoper,
careful there; your solution only works if you can guarantee that all the other bits (ie except for the least significant bit) of X1 to X8 are zero;
otherwise the output will be unpredictable

If the upper bits of X1 .. X8 can also contain other data, then you have to mask out everything but the lsb, eg

Total := (x1 & 1) << 7 | (x2 & 1) << 6 | (x3 & 1) << 5 | (x4 & 1) << 4 | (x5 & 1) << 3 | (X6 & 1) << 2 | (x7 & 1) << 1 | (X8 & 1) << 0

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
---
Joćo