Go language using binary conversion, bitwise operator
-
Binary : 0, 1, 2 into the full 1;
In the go, you can not use the binary to represent an integer.
-
Decimal : 0-9, into a full 10;
-
Octal : 0-7, 8 into full 1. Expressed with a digit 0;
-
Hex : 0-9 and AF, over 16 into 1. It represents the beginning with 0x or 0X;
AF case insensitive
For chestnut :
package main
import (
"fmt"
)
func main(){
var i int = 5
// 二进制输出
fmt.Printf("i 的二进制是 %b\n", i)
// 八进制: 0-7 满8进1, 以数字0开头表示
var j int = 011
fmt.Println("j=", j)
// 十六进制: 0-9 及 A-F, 满 16 进 1, 以0X 或 0x开头
var k int = 0x111
fmt.Println("k=", k)
}
// 输出结果
i 的二进制是 101
j= 9
k= 273
First, turn the other binary decimal:
1. Binary Coded Decimal:
Rules: From the lowest-order bit (on the right) , the number on each bit extracted, multiplied by (the number of bits -1) th power of 2 , then summed .
-
1011 binary to decimal:
1011 = 1 * 2(1-1) + 1 * 2(2-1) + 0 * 2(3-1) + 1 * 2(4-1)
1011 = 1 * 1 + 1 * 2 + 0 + 1 * 8
1011 = 1 + 2 + 8
1011 = 11
2. octal decimal turn:
Rule: From the lowest-order bit (the right side) , the data on each bit taken out, multiplied by 8 (the number of bits -1) th power , and then summed .
-
The octal to decimal format:
0123 = 3 * 8(1-1) + 2 * 8(2-1) + 1 * 8(3-1) + 0 * 8(4-1)
0123 = 3 + 16 + 64 + 0
0123 = 83
3. Hex Decimal turn:
Rules: From the lowest-order bit (on the right) , the data on each bit extracted, multiplied by (the number of bits -1) power of 16 , and then summed .
-
The 0x34A hexadecimal to decimal:
0x34A = 10 * 16(1-1) + 4 * 16(2-1) + 3 * 16(3-1)
0x34A = 10 * 1 + 4 * 16 + 3 * 16 * 16
0x34A = 842
Second, turn the other binary decimal:
1. Binary Decimal turn:
Rules: the number constantly divided by two until the quotient is zero so far , then every step to get the remainder upside down , is the corresponding binary.
-
Conversion from binary to decimal 56:
56 / I 2 = 280
28 / I 2 = 140
14 / I 2 = 70
7/3 I 2 = 1
3/2 = I 1 1
1 left
Last Result: 111000
2. Decimal turn octal:
Rules: the number constantly divided by 8, up until the quotient is zero , then every step to get the remainder upside down , is the corresponding octal.
-
156 will convert from decimal to octal:
156/8 = 194 I
19/8 = 2 3 I
2 left
Last result: 0234
3. decimal convert hexadecimal:
Rules: the number constantly divided by 16, up until the quotient is zero , then each step to get the remainder upside down , it is the corresponding hexadecimal.
-
356 will convert from decimal to hexadecimal:
356 / I 224 = 16
22/16 = I 16
1 left
Last result: 0x164
Third, Binary other band:
1. Binary octal:
Rule: A binary number of each set of three (from a combination of low tensile start) , converted to the corresponding octal number .
-
The binary 11010101 converted to octal;
11010101 three per group
101 = 1 * 2(1-1) + 0 + 1 * 2(3-1) = 1 + 0 + 4 = 5
010 = 0 * 2(1-1) + 1 * 2(2-1) + 0 = 0 + 2 + 0 = 2
11 = 1 * 2(1-1) + 1 * 2(2-1) = 1 + 2 = 3
Finally octal: 0325
2. Binary Hex:
Rule: The binary number (from the lowest bit combinations) of each set of four bits , converted into the corresponding hexadecimal number .
-
The binary 11010101 converted to hexadecimal
11,010,101 each set of four bits
0101 = 1 * 2(1-1) + 0 + 1 * 2(3-1) + 0 = 1 + 0 + 4 + 0 = 5
1101 = 1 * 2(101) + 0 + 1 * 2(3-1) + 1 * 2(4-1) = 1 + 0 + 4 + 8 = 13
Finally hex: 0xd5
Four other binary transfer binary:
1. octal binary transfer:
Rule: The octal per 1 , converted to the corresponding binary number is three .
-
Octal 0237 will turn into a binary;
0237
7/3 I 2 = 1
3/2 = I 1 1
1 left
7 corresponds to a
3位
binary number 1113/2 = I 1 1
1 left
3 corresponds to a
3位
binary number 0112/2 = I 1 0
2 corresponds to a
3位
binary number 010Results: 010,011,111
2. turn hex binary:
Rule: The number per one hexadecimal , converted to the corresponding binary number is a 4-bit .
-
Hexadecimal 0x237 converted into binary;
0x237
7/3 I 2 = 1
3/2 = I 1 1
1 left
7 corresponds to a
4 位
binary number 01113/2 = I 1 1
1 left
3 corresponds to a
4位
binary number 00112/2 = 1 1 I
Left 0
2 corresponds to a
4位
binary number 0010RESULTS: 1000110111
V. bitwise operators:
Operators | description |
---|---|
& | Bitwise AND operator . Function is involved in computing the phase of two binary numbers each corresponding to the.运算规则:同时为 1,结果为 1 ,否则为0 |
| | Bitwise OR operator . Function is involved in computing two binary numbers each corresponding phase, or.运算规则:有一个为1,结果为1,否则为0 |
^ | Bitwise exclusive OR operator . Function is involved in computing the number of each of the two corresponding binary or different.运算规则:当二进位不同时,结果为1,否则为0 |
<< | Left-shift operator . Each feature is that the full binary operand left << shift left a number of bits, 高位丢弃,低位补0 .左移n位就是乘以2的n次方 |
>> | Right shift operator . All the functions of each bit of the binary arithmetic right shift >> right a number of bits,右移n位就是除以2的n次方 |
1. The original code, anti-code complement to explain:
For signed terms:
-
The highest binary bit is the sign bit:
0表示正数,1表示负数
:1 - - -> [0000 0001] // positive number
-1 - - -> [1000 0001] // negative
-
A positive number of the original code, anti-code and complement all the same;
-
Negative = inverted its original code symbol bits unchanged, the other bit is inverted (0 -> 1, 1 -> 0);
1 - - -> original code [00000001] inverted [0000 0001] Complement [0000 0001]
-1 - - -> original code [10000001] inverted [111,111,110] Complement [1111 1111] // negative complement code + 1 = trans
-
Negative complement its inverse + = 1;
-
0 is inverted, complement is 0;
-
计算机运算时
They are to complement in operation .1 + 1 1 - 1 = 1 + (-1)
2. bitwise shift operators:
Bitwise Operators:
Bitwise AND (&) : two all 1, the result is 1, and 0 otherwise;
Bitwise OR (|) : There is a two to one, the result is 1, and 0 otherwise;
Bitwise exclusive OR (^) : one of two 0, a is 1, the result is 1, otherwise 0;
package main
import (
"fmt"
)
func main(){
// 位运算的演示
fmt.Println(2&3)
fmt.Println(2|3)
fmt.Println(2^3)
fmt.Println(-2^2)
}
// 输出结果
2
3
1
-4
2 & 3
Complement 2: 0000 0010
3 Complement: 0000 0011
2 & 300000010 (binary 2's complement, compared with the twos complement 3 digits are 1, it is 1)
00000010 converted to decimal 2 =
2 so that the output 2 & 3
2 | 3
Complement 2: 0000 0010
3 Complement: 0000 0011
2 | 3 0000 0011 (2's complement, compared with the complement of 3 digits, a is 1, 1)
00000011 to decimal = 3
So 2 | 3 output 3
2 ^ 3
Complement 2: 0000 0010
3 Complement: 0000 0011
300000001 ^ 2 (2's complement, the complement of 3, compared with the number of bits, a is 1, a is 0, 1 only)
0000 0001 1 to DEC =
Therefore the output 1 2 ^ 3
-2 ^ 2
-2 original code: 10000010
Trans -2 code: 11111101
-2 Complement: 1111 1110 (case of a negative inverted + 1 = complement, do not forget we are into a binary +1)
Complement 2: 0000 0010
-2 ^ 211111100 (2's complement, the complement of 3, compared with the number of bits, a is 1, a is 0, 1 only)
1111 1100 to
补码
- - - anti-rotation complement codes required Save l- - ->反码
1111 1011 - - - at the beginning of the operation is negative 1 - - ->原码
1000010010000100 Coded Decimal (not including the beginning of the 1, 1 represents the beginning of the negative) = -4
Shift operation:
Right shift operator (>>) : low overflow, sign bit unchanged, and supplemented with upper overflow sign bit;
Left shift operator (<<) : the symbol bit constant, low 0s;
package main
import (
"fmt"
)
func main(){
// 移位运算符
a := 1 >> 2
fmt.Println("右移位运算符a=",a)
c := 1 << 2
fmt.Println("左移位运算符c=", c)
}
// 输出结果
右移位运算符a= 0
左移位运算符c= 4
a := 1 >> 2
Binary 1: 00,000,001 to
右移动两位
-----> ----- 0000 0000> Results: 0
c := 1<< 2
Binary 1: 00,000,001 to
左移动两位
-----> 0000 0100 -----> Results: 4