user976874
user976874

Reputation: 21

How to replace LSB of a byte with a LSB of another byte

How to replace LSB of a byte with a LSB of another byte in c#.

Something like this

byte1 - 0 1 1 1 1 1 1 1

byte2 - 0 0 1 1 1 0 0 0

Now i want lsb of byte1 i.e "1" to be replaced by lsb of byte2 i.e "0" . So my final byte should be like this :

byte3 - 0 1 1 1 1 1 1 0

Upvotes: 2

Views: 392

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502476

Sounds like you want something like:

byte x = ...;
byte y = ...;

// Only bits 1-7 of x, and only bit 0 of y (counting bit 0 = LSB)
byte z = (byte) ((x & 0xfe) | (y & 1));

The cast is necessary because all of the operators are only defined for int and larger, so everything gets promoted to int.

Upvotes: 6

Related Questions