There are two examples of bit switching in the book Java: A Beginners Guide . In both cases, the author writes about switching the 6th bit but he demonstrates it on 16 digits. Both examples use bitwise operators for changing the letter case.
'a' & 65503
changes the char to 'A'
. It is described as switching the 6th bit off. The problem is the number 65503
equals 1111 1111 1101 1111
in binary. Hence the 11th digit/bit is switched off (he even shows the number there). 'a' | 32
'a' | 32
does the trick. The number 32 equals 0000 0000 0010 0000
in binary. In both cases, the change does make sense. I just don't understand why the author writes about the 6th bit. I would understand if it was for the 11th bit or 6th pair (in that case I would expect the turning it off completely as 00
or 11
.
Any clarification more than welcome.
The location of the 6th bit could be in a few locations, depending upon the convention you adopt:
The author should really have defined which of these he was using, for clarity (and may do, elsewhere in the text). But apparently, he means item 4.
0000 0000 0010 0000
65 4321
The 0's and 1's that make up a binary number are called bits. The bits start from the right and go left:
So 0010 0000
:
8th bit 7th bit 6th bit 5th bit 4th bit 3rd bit 2nd bit 1st bit
0 0 1 0 0 0 0 0
Decimal is read the same way as binary:
3754
in decimal:
(3 x 1000) | (7 x 100) | (5 x 10) | (4 x 1)
156
in binary = 10011100
(1 x 128)|(0 x 64)|(0 x 32)|(1 x 16)|(1 x 8)|(0 x 4)|(0 x 2)|(0 x 1)
In decimal you add a new column to to the start of the number (ie the right) when you reach a power of 10.
In binary you add a new column when you reach a power of 2.
Does this help explain it?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.