Hoo*_*och 1 .net c# logic bit-manipulation
我有一个字节,我需要替换最后(最不重要)的位.以下示例.
Original byte: xxxx0110
Replacement byte: 1111
What I want to get: xxxx1111
Run Code Online (Sandbox Code Playgroud)
Original byte: xxxx1111
Replacement byte: 0000
What I want to get: xxxx0000
Run Code Online (Sandbox Code Playgroud)
Original byte: xxxx0000
Replacement byte: 1111
What I want to get: xxxx1111
Run Code Online (Sandbox Code Playgroud)
Original byte: xxxx1010
Replacement byte: 1111
What I want to get: xxxx1111
Run Code Online (Sandbox Code Playgroud)
Original byte: xxxx0101
Replacement byte: 0111
What I want to get: xxxx0111
Run Code Online (Sandbox Code Playgroud)
value = (byte)( (value & ~15) | newByte);
Run Code Online (Sandbox Code Playgroud)
在~15创造一切的面具,除了最后4位; value & {that mask}取最后4位,然后| newByte将新数据中的位置于其位置.