Go Down

### Topic: bit shifting (Read 3642 times)previous topic - next topic

#### carbondudeoxide

##### May 02, 2013, 07:05 pm
hello

if i have two 4-bit bytes such as A:0110 B:1011  and i want to combine them into an 8 bit array but not adding just putting them together like so:

01101011

how would i do it?

i tried using bit shifting like so:

byte testb = B0110;
testb = testb << 4;

and from what i have read of bitshift it should have come out of the shifting looking like : 01100000

i then added the second binary of 1011 and in my logic it should have formed this 8 bit binary:  01101011

is this right or am i able to bit shift the 2nd 4-bit binary onto the end like so?

byte testb = B0110 << B1011;

i ahve tried both of these ways that i ahve said but it has not given me the correct result afterwards and i am unsure why :/

#### tobyb121

#1
##### May 02, 2013, 07:12 pm
Code: [Select]
`byte A=B0110;byte B=B1011;byte AB=(A<<4)|B;`
This should work

#### HazardsMind

#2
##### May 02, 2013, 07:13 pm
byte A = B0110;
byte B = B1011;

byte C = (A<<4) | B;
//C = B01101011

//Break down,

C= A<<4;
C |= B; // C = C | B
//C = B01101011

Edit, I was beaten to it.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

#### EEpEr

#3
##### May 02, 2013, 07:15 pm
You can try this one:

byte testA=B0110;
byte testB=B1011;

void setup(){
testB=testA<<4 | testB;
Serial.begin(9600);
Serial.println(testB,BIN);
}
void loop(){

}

the 2nd parameter of Serial.print can help a lot sometimes

#### carbondudeoxide

#4
##### May 02, 2013, 11:11 pm
OK and to further complicate this i have a byte array:

byte numbers[] = {'B0001','B0000','B1001','B1000','B0111','B0110','B0101','B0100','B0011','B0010','B1111'};

how would i combine two of these together in the same way e.g.

numbers[3] and numbers [5]

would it be:

byte testb = numbers[3] << 4 | numbers[5];

?

#### tobyb121

#5
##### May 02, 2013, 11:36 pm
Just like that, but your array definition is all wrong:
'B0001' is not the same as B0001, B0001 is an number type, 'B0001' is an ASCII char type and will give the value 'B', decimal 66 (he other characters are ignored)

#6
##### May 02, 2013, 11:36 pm
Plus, you need parentheses.  << has pretty low precedence.

Code: [Select]
`byte testb = (numbers[3] << 4) | numbers[5];`

#### carbondudeoxide

#7
##### May 02, 2013, 11:40 pm
ok i have tried these and if the 8th bit (thats what i belive it is) if the upper most bit (left hand side when writing it) is 0 then it is ommitted but i need to send it as an 8 bit for the i2c i have running so i need it to have 8 bits and not emmit them. how can i ensure it can be used with all 8?

#### tobyb121

#8
##### May 02, 2013, 11:52 pm

ok i have tried these and if the 8th bit (thats what i belive it is) if the upper most bit (left hand side when writing it) is 0 then it is ommitted but i need to send it as an 8 bit for the i2c i have running so i need it to have 8 bits and not emmit them. how can i ensure it can be used with all 8?

What?!? Maybe read that one back...

#### carbondudeoxide

#9
##### May 03, 2013, 12:09 am
basically once i have done the bit shifting if i get a 8-bit binary with a 0 as the last bit (mot left bit one in blue)  01001010 instead of it remaining on the end it is ommited so the binary is displayed as 1001010 instead of 01001010 because i am using this 8-bit binary for an i2c project i need it to have all 8 bits in it. so what i am looking to do is force it to be an 8 bit regardless of it having a 0 on the end

#### PaulS

#10
##### May 03, 2013, 12:13 am
Quote
so what i am looking to do is force it to be an 8 bit regardless of it having a 0 on the end

That leading zero is not PRINTED. That doesn't mean it doesn't exist.

#### Sembazuru

#11
##### May 03, 2013, 12:16 am

basically once i have done the bit shifting if i get a 8-bit binary with a 0 as the last bit (mot left bit one in blue)  01001010 instead of it remaining on the end it is ommited so the binary is displayed as 1001010 instead of 01001010 because i am using this 8-bit binary for an i2c project i need it to have all 8 bits in it. so what i am looking to do is force it to be an 8 bit regardless of it having a 0 on the end

Still not quite sure if I'm understanding on your second attempt to explain, but let me take a whack...

Where is it being omitted? If you are concerned about only 1001010 being contained in the variable instead of 01001010, don't. You defined the variable as a byte, it will always have 8 bits in it. Some output/display methods may strip leading zeros, but the contents of the variable will still be all 8 bits.
http://www.catb.org/jargon/html/I/I-didn-t-change-anything-.html

Go Up