 How to splice two bytes into an int?

So far this is what i got:

byte a = B11111111;
byte b = B00001111;
int d;

void setup() {
Serial.begin(9600);
d = a;

for(z = 8; z > 1; z--){ /*if i set it a z > 0 i get a weird value returned --> "11111111111111111111111100001110"*/
d = d << 1 ;
y = (b >> z) & 1;
d |= (y << 1) ;
}
Serial.println( d , BIN );
}

However my resulting serial print is “111111110000110” and NOT “1111111100001111” (as you can see everything goes well except for the last two bits), so what is happening?

Alright so ive made abit of progress by using uint32_t instead of int

byte a = B11111111;
byte b = B00001111;
uint32_t d;
bool y;

void setup() {
Serial.begin(9600);
d = a; //initialize d as byte a --> "11111111"
for(z = 8; z >= 0; z--){
d = d << 1 ; // shift d left by 1
y = (b >> z) & 1; //set the bool y equal to the Zth position in byte b
d |= (y << 1) ; //Shift in y as new bit in uint32_t d?
}
Serial.println( d , BIN );
}

EDIT: So now the result of the print is → “11111111000011110” and NOT “1111111100001111” as you can see the last bit should not exist! what is happening? (i also believe that uint32_t is being transformed into a bigger data holder, is this true?)

You can do that in a much easier way:

uint16_t c = ( (uint16_t)a << 8 ) | b;

@guix Thank you so much! wow that’s exactly what i want<3 (as you can see i’m pretty new with bit operations )

If you shift any byte 8 times, you get zero. You attempted to do that with b>>z.