Analysis of conversion from negative to binary in JAVA, conversion from negative to binary in java
I recently looked at the collection source code and found that ArrayDeque uses a large number of & Operations, which involves binary. I suddenly found that I had a bit fuzzy on the negative binary number and provided some support for this.
First, we need to have an understanding of the original code, reverse code, and complement code:
1. The so-called original code is a binary fixed-point representation, that is, the highest bit is the symbol bit, "0" indicates positive, "1" indicates negative, and the remaining bit indicates the value size. 2. Anti-code Notation: a positive number is the same as its original code. A negative number is the opposite of its original code, except for the symbol bit. Original code 10010 = reverse code 11101 (11101, 1 is a real number, so it is negative) () binary =-13 decimal 3, complement code representation rules: positive number complement code is the same as the original code; the complement of a negative number is to add 1 at the end of its anticode.
For example, let's look at how integer-1 is represented in a computer.
Suppose this is also an int type, then:
1. first obtain the original code of 1: 00000000 00000000 00000000 00000001
2. Reverse code: 11111111 11111111 11111111 11111110
3. Supplemental code: 11111111 11111111 11111111 11111111
Hexadecimal representation: 0 xFFFFFFFF
Therefore, the result of Integer. toBinaryString (-5) in Java is 11111111111111111111111111111011. Integer is 32-bit.
The binary rule of negative numbers:
1. Obtain the original code of the absolute value of the negative number;
2. Calculate the back code of the original code;
3. Add an ampersand to obtain the complement code.
Author: The Blue Drop Source: http://blog.csdn.net/cl05300629
How to convert a negative number into a binary string in java?
Int I =-3;
System. out. println (Integer. toBinaryString (I ));
Let's give you a question: how can I convert a negative decimal number into a binary number using a java program?
1. Convert the number to positive first. Then we use the Division 2 method to get the binary system.
2. Change the highest bit to 1 representing a negative number.
3. bitwise inversion. The one at the end of the bitwise plus one gets the complement code.