Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change 4 middle bits of a byte in C

I'm trying to change the 4 middle bits of a byte to correspond to the High nibble of another byte:

Suppose we start with:

In = 0bABCDEFGH
Out = 0bXXXXXXXX // Some random byte

I want:

Out = 0bXXABCDXX

Leaving whatever other bits were in Out's extremes unchanged.

How can I do this?

Note: The 'X' represents any bit, 0 or 1, just to distinguish what came from the input.

I got to:

(0b00111100 & (IN>>2)) = 0b00ABCD00

, which filters the high nibble and centers it but then what? How can I move it to Out?

like image 716
A. Vieira Avatar asked Aug 31 '25 01:08

A. Vieira


1 Answers

simple:

out &= 0b11000011;
out |= (in >> 2 & 0b00111100);

out &= 0b11000011 sets out to 0bxx0000xx preserving 2 most significant bits and 2 least significant bits. in >> 2 shifts input by 2 giving us 0xYYABCDEF, YY could be 00 or 11 depending on what A is. To get rid of YY and EF we do & 0b00111100.

As pointed by @JB 0B is not standard notation, thus you should use something else, most preferably hex 0x notation. See this for more info.

Thus using hex this would be:

out &= 0xC3;
out |= (in >> 2 & 0x3C)

here is conversion table

`0xf` is `0b1111`
`0x3` is `0b0011`
`0xc` is `0b1100`