Log in

View Full Version : Help putting a byte back together



Hylan
- 6th November 2007, 03:55
Okay I'm stuck and need some help. In my program I am polling a port for settings from a dip switch which I'm breaking into 2 numbers. The first 4 and second 4 bits. the code is:

ADDR = PortE ' Read Unit Address
UID = ADDR >> 4 ' Shift Upper 4 Bits of into Lower 4 Bits
LID = ADDR & %00001111 ' Mask-Off (set) Upper 4 Bits to Zero

Now that I've separated the data I need at this point out of ADDR, I want to rebuild it (ADDR) using two different 4 bit numbers. So lets say I now have variables
UID2 and LID2

How do I shift UID2 from the lower to the upper Bits and then combine it with the lower bits of LID2.

For example:
UID2 = 00001011
LID2 = 00001001
I want ADDR to look like 10111001

Thank you,
Hylan

Charles Linquis
- 6th November 2007, 04:30
UID2 = UID2 << 4 ' shift nybble back to original location.
LID2= UID2 | LID1 ' bitwise OR the two together.

Darrel Taylor
- 6th November 2007, 08:21
Semantics, spelling, good beer, not sure.

But maybe that should be, ...

ADDR = (UID2 << 4) | (LID2 & $F)
<br>

Charles Linquis
- 6th November 2007, 17:12
Actually, a trip to the Sonoma area with little sleep afterward!

Hylan
- 6th November 2007, 17:50
Thank you. I'm learning slowly through trial, error, some more error, studying and trying. This forum has been an invaluable source of information.