# Thread: Quick question regardin bit shifting

1. ## Quick question regardin bit shifting

im trying to shift two bytes of "binary" to OR them together and allow a carry from byte1 to be placed into byte2.

at the moment, im trying to shift byte1 by 7 places to the RIGHT and byte2 by 1 place to the LEFT.

i need to use the logical shift and not the arithmetic one so could someone look at the couple of lines of code below to tell me why, when i or together two bytes i get the wrong answer.

Code:
`firstbyte[0] = (squashdum[1] >>= 7);`
Code:
`secondbyte[0] = squashdum[2] <<= 1;`
Code:
`result[1] = (firstbyte[0] | secondbyte[0]);`
imagine first byte is 01100101
and second byte is 10000111

the result should be "00001110". = e0

but actually comes through as "00001111"? = f0

thanks

2. So what value does 'squashed' have in it first? At any rate:

01100101 >> 1 = 00110010
00110010 >> 1 = 00011001
00011001 >> 1 = 00001100
00001100 >> 1 = 00000110
00000110 >> 1 = 00000011
00000011 >> 1 = 00000001

Seven shifts.

10000111 << 1 = 00001110

One shift.

00001110 | 00000001 = 00001111

See?

Quzah.

3. Originally Posted by quzah
Seven shifts.
Looks like 6.

4. Hm. Could be. I was thinking when I was working it out that their end result was wrong. Looks like I was right after all.

So yeah, anyway, your:
a) Input is wrong, or...
b) Your output is wrong, as to what you're telling is you're getting.

Because if you did shift that seven places, and not 6, your result would be zero for the first portion.

Quzah.

5. well.

im inputting a string of hexcharacters "1234567890" into a function which "squashes" them so that
Code:
```squashdum[0] = "00010010" = "12"
squashdum[1] = "11000010" = "34"```
and pairs of numbers/hex thereafter.

the MSB in squashdum[0] must be Or'd with squashdum[1]. at the moment, im doing only ONE shift.

im starting to think that its maybe to do with the numbering of the indexes of the array and the arrangement of the binary stack within it.

i.e. im thinking that the string in contains the values as...

Code:
`squashdum[2] squashdum[1] squashdum[0]`
by my logic would equate to a "binary" of within the array

Code:
`01100101 ||| 01000011 ||| 00100001`
the aim is to insert a zero or maybe 5 zero's at the start of the string "squashdum[0]" and watch the bits move along. if they fall off the edge then they just move to the next byte.

is this correct or have i confused myself?

6. I'm having trouble with:
Code:
`squashdum[1] = "11000010" = "34"`
Wouldn't that binary number represent the two 4-bit integers 12 (0xC) and 2?

1 and 2 in squashdum[0] looks right, but that one looks weird. 3 and 4 should be 00110100.

7. i just spoke to a friend and he told me the order of bytes and power of bits is:

[0][1][2][3]

within which is

[0]MSB.........LSB[0] [1]MSB.........LSB[1]

hope this is right now!

8. You can express binary numbers either way, but you have to express them consistently or you're going to get confused. squashdum[0] shows MSB -> LSB, but squashdum[1] shows LSB -> MSB. Typically I've seen MSB -> LSB so you have:

1 - 0001
2 - 0010
3 - 0011
etc.

9. Originally Posted by itsme86
You can express binary numbers either way, but you have to express them consistently or you're going to get confused. squashdum[0] shows MSB -> LSB, but squashdum[1] shows LSB -> MSB. Typically I've seen MSB -> LSB so you have:

1 - 0001
2 - 0010
3 - 0011
etc.
excellent.

its just that in my earlier post, i had them the wrong way round.

i was actually asking if i was correct!....which i wasnt

thanks alot though.

10. i've found that the problem is that when i shift the following string left by 1 position or multiply it by two:

i get the answer F0 when it should be E0. i think the program is adding and an extra 1 instead of a 0 at the least significant bit, after the shift.

so.....

Code:
`1000 0111`
should be

Code:
`0000 1110`
after 1 shift left.

but i get

Code:
`0000 1111`
how do i set the LSB to zero? was thinking of an AND mask with 0xFE but would that work?

11. I get the right answer. I'm not sure what you're doing wrong...
Code:
```itsme@itsme:~/C\$ ./shift
10000111 - 87
00001110 - 0E
itsme@itsme:~/C\$ cat shift.c
#include <stdio.h>

void show_it(int num)
{
int i;

for(i = 7;i >= 0;--i)
printf("%c", (num >> i) & 1 ? '1' : '0');
printf(" - %02X\n", num);
}

int main(void)
{
unsigned char num = 0x87;

show_it(num);
num <<= 1;
show_it(num);

return 0;
}```
Code:
```itsme@itsme:~/C\$ ./shift
10000111 - 87
00001110 - 0E
itsme@itsme:~/C\$```
how do i set the LSB to zero? was thinking of an AND mask with 0xFE but would that work?
Yes, but you shouldn't need it. I don't see how that 1 is slipping in.