bit shifting

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 :confused:

byte A=B0110;
byte B=B1011;
byte AB=(A<<4)|B;

This should work

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.

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 :slight_smile:

OK and to further complicate this :smiley: 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];

?

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)

Plus, you need parentheses. << has pretty low precedence.

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

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?

carbondudeoxide:
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... :wink:

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

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.

carbondudeoxide:
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.