1. What is a binary
Binary is a way of counting, commonly used in binary, octal, decimal, hexadecimal. Any data is stored in a binary form in the computer's memory.
My personal understanding of the system is that the binary number is calculated as 2, full 2 in 1 digits, and the octal number is calculated as 8, and the number is 8 in 1 bits.
For any number, we can use a different binary to represent, for example, the decimal number 12, with a binary representation of 1100, with an octal representation of 14, in hexadecimal representation of 0xC.
2. Conversion rules for Binary
Following the principle that the full-scale value goes into 1 bits and the single digit becomes 0, let's take the decimal number 18 as an example, and make a detailed description of each of the numeric conversions in 1-18.
Turn binary:
1 less than 2, no need to enter 1 bits, 1 binary value is 1
2 is the binary value 1 after a number, because the full 2, the need to enter 1 bits, the single digit becomes 0, so the binary value 2 is 10
3 is the binary value 10 after a number, because 11 of the single digit 1 is less than 2, do not need to enter 1 bits, so 3 binary value is 11
4 is the binary value 11 after a number, because 11 of the single-digit 2, the need to enter 1 bits, and the binary value 11 of the number of bits is full 2, so the number of bits plus 1, the final conversion result is 100
Conversion ideas: Binary Value 11+1->10+ (single digit equals 2, into 1 bits, single digit to 0)---(+) +0 (digits full 2, 1 bits), 100
And so on, the result of the binary conversion of the final decimal number 18 is 10010
Goto octal:
1-7 is less than 8, no need to enter 1 bits, 1-7 octal is represented by 1-7
8 is the octal value 7 after a number, because 7+1 8, requires 1 bits, the single digit becomes 0, so the octal value of 8 is 10
And so on, the result of the octal conversion of the final decimal number 18 is 22
Turn hex
hexadecimal, single digit 1-15 is 123456789abcdef(a=10....f=15)
16 is the hexadecimal value, followed by 1 digits, because c+1 is 16, requires 1 bits, the single digit becomes 0, so 16 hexadecimal is 10.
The final decimal number 18 of the 16 binary conversion result is 12
The detailed results are as shown (the C language puts the number in front of the number 0x as hexadecimal number)
declarations and placeholders for int types in the 3.C language
Although the following 3 variables are assigned in different ways, the actual assignment result is 18
Copy CodeThe code is as follows://binary type number plus 0b int number1 = 0b10010; Octal type number plus 0 int number2 = 022; Hexadecimal type number plus 0x int number3 = 0x12;
Eight-decimal placeholder:%o
Hex Placeholder:%x
4. Memory Storage Data Details
We know that the int type data occupies 4 bytes and 1 bytes is 8bit. And any data in the computer memory is stored in binary form, so memory needs 32 0 or one to describe 1 int type data.
Since the binary number of 18 is 10010, we assign an int type variable 18, which essentially modifies the 32 bit bits corresponding to the memory address of the variable to:
0000 0000 0000 0000 0000 0000 0001 0010 (less than 31 digits, followed by 0 padding: Why 31 instead of 32, as described later)
Let's say we define a two variable
?
123456 |
//二进制类型数字加0b int number1 = 0b10010; //八进制类型数字加0 int number2 = 022; //十六进制类型数字加0x int number3 = 0x12; |
The computer allocates memory space based on the memory address in the order of large to small, as shown in the following:
5. Conversion formula of the binary
Binary goto Decimal
0b1100->0*2 0 times + 0*2 1 square + 1*2 2-square + 1*2 3-Time Square = 12
Decimal Turn binary
->64+2+1->2 6 times + 2 of 1 parties + 2 0 times = 0b1000011
6. Additional knowledge of the binary
1.N-bit binary can save an integer range formula: 2 of the N-th square-1
For example, the 3-bit binary has a maximum value of 111, the corresponding decimal digit is 7, the 5-bit binary maximum is 11111, and the corresponding decimal number is (2*2*2*2*2)-1 = 31.
2. The binary save rule for negative numbers is the leftmost number is 1. For example, 0000 0000 0000 0000 0000 0000 0001 0010 Indicates a positive integer, 1111 1111 1111 1111 1111 1111 1110 1101 indicates negative numbers
Thus, we can infer that the largest integer that the int type can hold is 2 (32-1) of the square-1 = 2147483647. Why use 32-1, very simple, 32 bit, must draw 1 bit bit to describe whether this number is positive or negative.
A dynamic-link library called C + +