Secondly, there is a mistake in your understanding: <<The right shift operator shifts the parameter on the left side of the operation to the right by the parameter position on the right side. In other words, 1 << bit_number does not shift bit_number to the right 1 bit, but shift 1 to the right by bit_number bit. Give your example, when bit_number is 3, the result of 1 << bit_number is 00001000. Then do the AND operation with value of 01011100, and you can change the 4 position from the right to 1.
The designated position is a certain digit, which refers to the bit_number+1th digit
1 << The bit_number is 0000 0001 (assumed to be 8 bits) and the bit_number bit is shifted to the left, rather than the bit_number is shifted to the left by one bit. If you are not familiar with the basic concept, let’s take a look at the basics first. . . .
1 << bit_number shifts 1 to the left by bit_number bits, so the result is a binary number containing only one 1 (its bit_number bit from right to left is 1). For example, 1 << 3 gets The binary number is 1000.
will be used for bitwise AND operation with value in the future, and the effect should be clear at a glance.
First of all,
<<
has a higher priority than|
.Secondly, there is a mistake in your understanding:
<<
The right shift operator shifts the parameter on the left side of the operation to the right by the parameter position on the right side. In other words,1 << bit_number
does not shiftbit_number
to the right1
bit, but shift1
to the right bybit_number
bit.Give your example, when
bit_number
is3
, the result of1 << bit_number
is00001000
. Then do the AND operation withvalue
of01011100
, and you can change the4
position from the right to1
.The designated position is a certain digit, which refers to the
from right to left.bit_number+1
th digithttp://jb51.net/article/37282.htm
Priority
<<
has a higher priority than|
. After the operation, only one bit ofvalue
will change at most.The function of this expression is to set the
value
th ofbit_number
to 1.For example:
value = 01010100, bit_number = 3
, the result of the operation is(01010100 | 1000) = 01011100
1 << The bit_number is 0000 0001 (assumed to be 8 bits) and the bit_number bit is shifted to the left, rather than the bit_number is shifted to the left by one bit. If you are not familiar with the basic concept, let’s take a look at the basics first. . . .
<<
has a higher priority than|
.1 << bit_number
shifts 1 to the left by bit_number bits, so the result is a binary number containing only one 1 (its bit_number bit from right to left is 1). For example,1 << 3
gets The binary number is 1000.will be used for bitwise AND operation with
value
in the future, and the effect should be clear at a glance.