10-Bit Manipulation

主要:十进制和二进制转换 Java二进制,0b开头

一、Positional number system | The Hindu-Arabic numeral system, | Base 10 | |----------------------------------|---------| | The ancient Babylonians | Base 60 | | The Mayans | Base 20 |

二、Converting base 1,To convert a number in base 10 to any other base, we need to figure out how many units of each power it has 案例

E.g. convert 1004 from base 10 to base 12

![image1](../../assets/ba35ca0c15c540c79b7735fbd5f3b69e.png)

• So the answer is 6-11-8 or 6elv8

![image2](../../assets/bbbeab6cc5ef4675b3c1f73918037ce6.png)

三、Bit Manipulation 1,In Java an int is represented as 32 bits • Starting from the right, each bit represents increasing powers of 2 • The leftmost bit is special. MSB 0101100101 is 357 image3 2,

![image4](../../assets/86f788b17ab741bd86941d9bb0740426.png)

![image5](../../assets/4d2e961f9d90486381ea94b4ea3b58c9.png)

image6

image7 3,operator 3.1,基本 | image8 | image9 | image10 | |--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|

案例 | 2 & 2=2 | |-----------| | 8|7=15 | | 11^3=8 | | 11^-3=-10 |

3.2,左移乘,右移除

1) Bitwise left shift(\<\<)
image11
Bitwise signed right shift (>>) Bitwise unsigned right shift (>>>)
image12 image13

3.3,Bitwise complement (~) 取反 ~n=-(n+1); image14

2 \<\< 3=16
8 >> 2=2
~7=-8
~-4=3
~4=-5

-11 >>> 1=2147483642 given that -1 >>> 1 is 2,147,483,647 ~n=-(n+1);n>0 | image15 | ~4=-5 | |------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------| 三、Interview question 1, image16

![image17](../../assets/5afa03b8a7f14ff7938415469471e064.png)

![image18](../../assets/117cedbc33f34492b043bb372c62b625.png)

![image19](../../assets/e6447193128d48e1a2f7fc1b97ada28d.png)

![image20](../../assets/35a1046002bf49909e0a098a4e114718.png)

image21

image22