Pages: [1]   Go Down
Author Topic: bit shifting  (Read 797 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :/
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
byte A=B0110;
byte B=B1011;
byte AB=(A<<4)|B;
This should work
Logged

Queens, New York
Online Online
Faraday Member
**
Karma: 98
Posts: 3575
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Created Libraries:
NPV2 (NewPasswordV2),  TFT_Extension, OneWireKeypad, SerialServo.
Will provide libraries if asked in PM or forum.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

?
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Offline Offline
God Member
*****
Karma: 25
Posts: 526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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... smiley-wink
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49077
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Mid-Atlantic, USA
Offline Offline
God Member
*****
Karma: 30
Posts: 515
"Remember kids, the only difference between Science and screwing around is writing it down." - Adam Savage
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Pages: [1]   Go Up
Jump to: