C language-based knowledge summary, C language-based summary

Source: Internet
Author: User
Tags binary to decimal decimal to binary

C language-based knowledge summary, C language-based summary

1. What is hexadecimal

Hexadecimal is a counting method, which is commonly used in binary, octal, decimal, and hexadecimal notation. Any data is stored in binary in computer memory.

In my personal understanding of the hexadecimal system, the binary number is the number of 2 as the computing unit, full 2 in the first place; the eight-digit number is the number of 8 as the computing unit, full 8 in the first place.

For any number, we can use a different hexadecimal representation. For example, the decimal number 12 is represented as 1100 in binary format and 14 in octal format, it is represented as 0xC in hexadecimal notation.

 

2. hexadecimal conversion rules

Follow the principle that the full hexadecimal value is entered into one digit and the single digit is changed to 0. The following uses the decimal number 18 as an example to describe how to convert each numeric value from 1 to 18.

To binary:

1 is less than 2, and 1 bit is not required. The binary value of 1 is 1.

2 is the number next to binary value 1. Because 1 + 1 is full 2, you need to enter 1 bit and the single digit is changed to 0. Therefore, the binary value of 2 is 10.

3 is the number next to the binary value 10. Because the single digit 1 of 11 is less than 2, there is no need to enter 1 bit. Therefore, the binary value of 3 is 11.

4 is the number next to the binary value 11. Because the single digit 1 + 1 of 11 is full 2, it needs to enter 1 bit, while the double digit 1 + 1 of the binary value 11 is full 2, therefore, when the number of digits is increased by 1, the final conversion result is 100.

Conversion logic: the binary value is 11 + 1-> 10 + (1 + 1) (the single digit equals 2, the first digit, and the single digit changes to 0)-> (1 + 1) + 0 (the number of digits is 2, and the number is 1)-> 100

Similarly, the final Binary Conversion Result of decimal number 18 is 10010.

 

To octal:

1-7 is less than 8 and does not need to enter 1 digit. The octal values of 1-7 are represented by 1-7.

8 is the number next to octal value 7. Because 7 + 1 is full 8, you need to enter 1 digit and the single digit becomes 0. Therefore, the octal value of 8 is 10.

Similarly, the result of the octal conversion of the decimal number 18 is 22.

 

Convert to hexadecimal

In hexadecimal notation, numbers 1-15 are 1 2 3 4 5 6 7 8 9 a B c d e f (a = 10... f = 15)

16 is the number of digits after the hexadecimal value c. Because c + 1 is full of 16, you need to enter 1 digit and the single digit becomes 0. Therefore, the hexadecimal value of 16 is 10.

The hexadecimal conversion result of the final decimal number 18 is 12.

Shows the detailed results (C considers the number prefixed with 0x as a hexadecimal number)

3. Declaration and placeholder of int type in C Language

Although the assignment methods of the following three variables are different, the actual assignment results are 18

// Add a binary number to 0b int number1 = 0b10010; // Add a octal number to 0 int number2 = 022; // Add a hexadecimal number to 0x int number3 = 0x12;

Octal placeholder: % o

Hexadecimal placeholder: % x

 

4. Memory storage data details

We know that int data occupies 4 bytes, and 1 byte is 8 bit. In addition, any data is stored in the computer memory in binary format. Therefore, the memory must use 32 zeros or 1 to describe 1 int type data.

Since the binary number of 18 is 10010, we assign an int type variable to 18. In essence, we change the 32 bit bits corresponding to the memory address of this variable:

0000 0000 0000 0000 0000 0000 0001 0010 (less than 31 digits, followed by digits filled with 0: Why 31 instead of 32, which will be introduced later)

Suppose we define two variables.

    int number1 = 12;    int number2 = 13;

 

The computer allocates memory space in the ascending order of memory addresses, as shown in:

5. hexadecimal conversion formula

Convert binary to decimal

0b1100-> 0*2 0 to the power + 0*2 to the power of 1 + 1*2 to the power of 2 + 1*2 to the power of 3 = 12

Decimal to binary

67-> 64 + 2 + 1-> 6 to the power of 2 + 1 to the power of 2 + 0 to the power of 2 = 0b1000011

 

6. Other hexadecimal knowledge

1. n-bit binary can save the Integer Range formula: n-1 of 2

For example, the maximum value of the three-digit binary number is 111, and the corresponding decimal number is 7; the maximum value of the five-digit binary number is 11111, the corresponding decimal number is (2*2*2*2*2)-1 = 31.

2. The left-side number of the negative binary storage rule is 1. For example, 0000 0000 0000 0000 0000 0000 0001 0010 represents a positive integer, and 1111 1111 1111 1111 1111 1111 represents a negative number.

 

As a result, we can infer that the maximum integer that the int type can store is 2 (32-1) to the power-1 = 2147483647. Why 32-1 is used? In 32 bits, one bit must be used to describe whether the number is a positive number or a negative number.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.