Pages: [1]   Go Down
 Author Topic: bit shifting  (Read 519 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 14
 « on: May 02, 2013, 12:05:48 pm » Bigger Smaller 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
Sr. Member
Karma: 9
Posts: 254
 « Reply #1 on: May 02, 2013, 12:12:18 pm » Bigger Smaller Reset

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

Queens, New York
Online
Karma: 59
Posts: 2732
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
 « Reply #2 on: May 02, 2013, 12:13:23 pm » Bigger Smaller 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

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

0
Offline
Newbie
Karma: 0
Posts: 7
Arduino rocks
 « Reply #3 on: May 02, 2013, 12:15:25 pm » Bigger Smaller 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
 Logged

Offline
Newbie
Karma: 0
Posts: 14
 « Reply #4 on: May 02, 2013, 04:11:01 pm » Bigger Smaller Reset

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];

?
 Logged

Offline
Sr. Member
Karma: 9
Posts: 254
 « Reply #5 on: May 02, 2013, 04:36:07 pm » Bigger Smaller 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
Sr. Member
Karma: 19
Posts: 486
 « Reply #6 on: May 02, 2013, 04:36:59 pm » Bigger Smaller Reset

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

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

Offline
Newbie
Karma: 0
Posts: 14
 « Reply #7 on: May 02, 2013, 04:40:19 pm » Bigger Smaller 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
Sr. Member
Karma: 9
Posts: 254
 « Reply #8 on: May 02, 2013, 04:52:36 pm » Bigger Smaller 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...
 Logged

Offline
Newbie
Karma: 0
Posts: 14
 « Reply #9 on: May 02, 2013, 05:09:53 pm » Bigger Smaller 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
Brattain Member
Karma: 517
Posts: 44538
Seattle, WA USA
 « Reply #10 on: May 02, 2013, 05:13:31 pm » Bigger Smaller 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
Sr. Member
Karma: 19
Posts: 391
"Remember kids, the only difference between Science and screwing around is writing it down." - Adam Savage
 « Reply #11 on: May 02, 2013, 05:16:30 pm » Bigger Smaller 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