c# byte removes the highest bit, the highest bit becomes 0

This article demonstrates with hexadecimal numbers, which is actually a bitwise AND.
The first thing to be clear is that there is only binary in the computer. The octal, decimal, and hexadecimal we see are all converted by the compiler for us to see. Therefore, to remove the highest bit, we only need to perform a bitwise AND on the highest bit. Can.


For example, operate on 0xE8, change the highest bit to 0,
first the binary representation of E8 is 1110 1000,

Just perform bitwise AND operation with 0111 1111
1110 1000
&
0111 1111
result
0110 1000

Use the code expressed as

byte b1 = 0xe8;
b1 = (byte)(b1 & 0x7f); //与 0111 1111 按位与,让最高位变成0

By analogy, as long as the value of the binary digital template of the bitwise AND is modified, we can change any bit to 0 at will.
If you don’t know the hexadecimal value corresponding to the binary template, use the calculator that comes with windows (win+R, enter calc), change the standard mode to programmer mode, you can directly enter binary, and it will automatically convert to other bases

insert image description here

Guess you like

Origin blog.csdn.net/weixin_44568736/article/details/131063621
Bit
BIT