Negative Binary Representation-"Struggle Cup" Programming Contest Topic
0, title
The numbers in the computer are all binary representations. In fact, there is a negative binary representation, which does not require a sign bit.
For example, the negative binary representation of 3 is 111, because (-2) 2 + (-2) 1 + (-2) 0 = 4-2+1 = 3 .
Another example is the negative binary representation of -3 is 1101, because (-2) 3 + (-2) 2 + (-2) 0 = -8+4+1 = -3 .
It is required to input an integer and output its negative binary representation.
- Golang
- Java
1. Thinking
power | value |
---|---|
0 | 1 |
1 | -2 |
2 | 4 |
3 | -8 |
4 | 16 |
5 | -32 |
It can be seen from the table that negative binary can theoretically represent any integer value within the range of computer digits.
Prepare a few special values for future use,(0)10 = (0)-2 , (1)10 = (1)-2 , (-1)10 = (11)-2
First recall the method of converting decimal to binary,Divide by 2 and write the remainder backwards, Um, let's try it first and see if we can also use negative binary conversion.
1.1 First try a simple value 16
- 16/(-2) = -8 ······ 0
- -8/(-2) = 4 ······ 0
- 4/(-2) = -2 ······ 0
- -2/(-2) = 1 ······ 0
So (16) 10 = (10000) -2 , well, it seems to be fine.
1.2 Try again the value 3 in the question stem
- 3/(-2) = -1 ······ 1
- -1/(-2) = 0 ······ -1
- -1/(-2) = 0 ······ -1
- ······
- -1/(-2) = 0 ······ -1
Uh, how did it become an infinite loop, although according to the previous special value (-1) 10 = (11) -2 , it can be deduced that the 3/(-2) quotient is -1 and the remainder is 1 , so (3) 10 = ( 111) -2 , it seems to be done.
1.3 Try the special value-13
Run -13/(-2) in the code first, and get a quotient of 6 and a remainder of -1 . Wait, what the hell is the remainder being -1? Shouldn't the remainder be 1 or 0? Think about it again, the quotient that should be obtained is 7, and the remainder is 1 . then:
- -13/(-2) = 7 ······ 1
- 7/(-2) = -3 ······ 1
- -3/(-2) = 2 ······ 1
- 2/(-2) = -1 ······ 0
- -1/(-2) = 1 ······ 1
Therefore, (-13) 10 = (110111) -2 .
Check the calculations, (-2) 5 +(-2) 4 +(-2) 2 +(-2) 1 +(-2) 0 = -32+16+4-2+1 = -13. Nothing wrong!
In conclusion, when the dividend is negative and the remainder is -1, the calculated quotient should be increased by one, and the remainder becomes positive 1 .
2 Code implementation of go and java
//go语言实现
func intToNegativeBinary(n int) {
if n == 0 {
fmt.Println(0)
return
}
const BASE = -2
var ans []byte
for n != 1 {
if n > 0 {
ans = append(ans, byte('0'+n%BASE))
n /= BASE
} else {
yu := n % BASE
if yu == -1 {
//余数为-1
n = n/BASE + 1
ans = append(ans, '0'+byte(1))
} else {
ans = append(ans, '0'+byte(yu))
n /= BASE
}
}
}
//把最后剩下的1加进去
ans = append(ans, '0'+byte(1))
length := len(ans)
//反转
for i := 0; i < length/2; i++ {
ans[i], ans[length-1-i] = ans[length-1-i], ans[i]
}
fmt.Println(string(ans))
}
//java代码实现
public static void intToNegativeBinary(int n){
if (n == 0){
System.out.println(0);
return;
}
int BASE = -2;
StringBuilder str = new StringBuilder();
while (n != 1){
if (n > 0){
str.append(n%BASE);
n/=BASE;
} else {
int yu = n%BASE;
if (yu == -1){
//余数为-1
n = n/BASE + 1;
str.append(1);
} else {
str.append(n%BASE);
n/=BASE;
}
}
}
//把最后剩下的1加进去
str.append(1);
str.reverse();
System.out.println(str);
}
For java code, you can also use str.insert(0, 1); instead of append first and then reverse, to achieve append to the front.
I tested it myself. When there is a large amount of data, such as the magnitude of 10 5 , the time difference between the two is close to 20 times, so it is faster to append and then reverse.