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 by开发者_如何转开发te 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
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
.
精彩评论