C-Language Knowledge summary _c language

Source: Internet
Author: User

1. What is in the system

The system is a kind of counting way, commonly used in binary, octal, decimal, hexadecimal. Any data is stored in binary form in the computer's memory.

I have a personal understanding of the system, the binary number is 2 for the calculated unit, full 2 into 1 digits; the octal number is 8 for the calculated cell and 8 in 1 digits.

For any number, we can use different representations, such as decimal number 12, binary representation of 1100, octal to 14, and hexadecimal as 0xC.

2. Conversion rules of the system

Follow the full value into 1 digits, the number of digits into 0 principle, the following we take the decimal number 18 as an example, to 1-18 of each of the numerical conversion of the system to make a detailed description

Binary conversion:

1 less than 2, no need to enter 1 bits, 1 binary value is 1

2 is a binary value of 1 after a number, because the 1+1 full 2, need to enter 1 digits, single-digit to 0, so the 2 binary value is 10

3 is the binary value 10 after a number, because 11 of Single-digit 1 is less than 2, do not need to enter 1 digits, so 3 binary value is 11

4 is a binary value of 11 after a number, because 11 of Single-digit 1+1 full 2, need to enter 1, and the binary value of 11 digits 1+1 and 2, so the number of digits plus 1, the final conversion result is 100

Conversion mentality: Binary value 11+1->10+ (1+1) (Single-digit equals 2, into 1 digits, single-digit to 0)-> (1+1) +0 (digits full 2, into 1)-> 100

And so on, the result of the binary conversion of the final decimal number 18 is 10010.

Turn octal:

1-7 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 the 7+1 full 8, need to enter 1 digits, single-digit to 0, so 8 octal value is 10

And so on, the result of the octal conversion of the final decimal number 18 is 22.

Turn hexadecimal

In hexadecimal, the single-digit number 1-15 is 123456789abcdef(a=10....f=15), respectively.

16 is the hexadecimal value C after the 1 number, because the c+1 full 16, need to enter 1 bits, single-digit to 0, so 16 hexadecimal is 10.

The final decimal number 18 of the 16 conversion result is 12

The detailed results are shown in the following figure (C language is considered to be a hexadecimal number before the number 0x)

Declaration and placeholder for int type in 3.C language

Although the following 3 variables are assigned in different ways, the actual assignment results are 18

Copy Code code 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;

Octal placeholder:%o

Hexadecimal 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 is stored in binary form in the computer's memory, so the memory needs to use 32 0 or one to describe the 1 int type data.

Since the binary number of 18 is 10010, we assign the value of an int type variable to 18, essentially modifying the 32 bit bits corresponding to the memory address of this variable to:

0000 0000 0000 0000 0000 0000 0001 0010 (not full 31 digits, the following number is filled with 0: Why 31 instead of 32, which is described later)

Let's say we define two variables.

Binary type number plus 0b
  int number1 = 0b10010;
  Octal type number plus 0
  int number2 = 022;
  Hexadecimal type number plus 0x
  int number3 = 0x12;

The computer allocates memory space in a large to small order based on the memory address, as shown in the following illustration:

5. Conversion formula of the system

Binary Turn Decimal

0b1100->0*2 of 0 times + 0*2 1 square + 1*2 2 square + 1*2 3 times = 12

Decimal conversion Binary

->64+2+1->2 6 times + 2 of 1 square + 2 0 Times Square = 0b1000011

6. Other knowledge of the system

1.N bit binary can save integer range formula: 2 N-TH-1

For example, the 3-bit binary maximum is 111, the corresponding decimal number is 7, and the 5-bit binary maximum is 11111, and the corresponding decimal digit 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 is a positive integer, 1111 1111 1111 1111, 1111 1111, 1110 1101 represents a negative number

As a result, we can infer that the maximum number of integers that can be saved by the int type is 2 (32-1) of the secondary-1 = 2147483647. Why use 32-1, very simple, 32 bit, must smoke 1 bit bits to describe whether this number is positive or negative.

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.