The previous talk briefly introduces constants and variables, which add a little bit to the basics of computer---binary.

Let's take a look at how the usual expression of an integer, the most common must be expressed in Arabic numerals, such as "12", we can say with 12来, in fact, this representation is based on a kind of called "decimal" Count way. "Binary" is a way of counting, there are 4 kinds of the common system in programming: decimal, Binary, octal, hex. In other words, we have at least 4 representations of the same integer.

First, the decimal 1. Concept

Decimal is our most familiar, most commonly used a counting method, it has two features: 0, 1, 2....9 10 basic figures, the operation rule is "every ten into one."

2. Operations

The so-called "every ten into one" seems to have been learned in primary mathematics, that is, when the value of 10 o'clock, it is necessary to move forward a

The single digit is 9+1, full ten, 10 digits in one.

Back to top two or two binary 1. Concept

Binary is a count of the internal use of the computer, it has two features: 0, 12 basic numbers, the operation rule is "every two in one."

2. Details

1> One might ask: why is there no 2~9 these numbers in binary? All said, every two in one, so when the number is two, it will carry.

2> If I write a 1010, can you see whether it is binary or decimal? In order to differentiate from other binary systems, it is necessary to start with 0b or 0B when writing binary numbers. For example, 0b1010 is a binary number, and 1010 is still familiar with the decimal number, that is, "1010"

3. Operations

Let's do a simple operation, like a

The single digit is the same, the value is full two, so 10 digits to enter one. Therefore, in binary: 1 + 1 = 0b10, here the 0B10 represents "two" in decimal, not "ten".

And so on, one + 1 = 0b100

4. Binary into decimal

1> in binary notation, although simple, convenient, but not easy to read, such as give a random binary number 0b110111101010, you can immediately see what it represents the value of it? It is generally necessary to convert a binary number to a decimal number before you know what value is represented.

There is a "cardinality" concept in the system, the cardinality is used to calculate the value, such as the base of the decimal is 10, so the decimal is this:

1230 = 0 * 100 + 3 * 101 + 2 * 102 + 1 * 103 = 0 * 1 + 3 * 10 + 2 * 100 + 1 * 1000

The cardinality of the binary is 2, and so on:

0b1011 = 1 * 20 + 1 * 21 + 0 * 22 + 1 * 23 = 1 * 1 + 1 * 2 + 0 * 4 + 1 * 8 = 1 + 2 + 0 + 8 = 11

So the numeric value represented by the binary number 0b1011 is 11 in decimal.

2> a binary number can represent a maximum value of 1, while n-bit binary number can represent the maximum value is 0b111 ... 1111, which means that the n-bit binary number is 1, then its decimal value is:

1 * + 1 * + 1 * + 1 * + + ... + 1 * 2n-1 = 2n-1

Therefore, the maximum value that can be represented by the N-bit binary number is 2n-1. That is, the maximum value that the 4-bit binary can represent is 0b1111, the decimal value is: 24-1 = 15, and the 5-bit binary number can represent a maximum of 25-1 = 31.

Back to top three or eight binary 1. Concept

The octal system has two features: The 0~7 consists of eight basic numbers; the arithmetic rule is "every eight into one".

2. Details

Since 0~7 is included in both decimal and octal, in order to distinguish between octal numbers, you need to add a 0 in front of it. For example, 076 is an octal number, and 76 is a decimal number.

3. Operations

The single digit is 7+1, the value is full eight, so 10 digits to enter one. Therefore, in octal, 7 + 1 = 010. 010 means "Eight" in decimal, not "ten"

4. Eight binary decimal

The base of octal is 8, so 027 is calculated as 23 in decimal.

027 = 7 * 80 + 2 * 81 = 7 * 1 + 2 * 8 = 23

5. Binary Turn octal

It is not difficult to find a decimal number can represent the maximum value is 9, and an octal number can represent the maximum value is 7, exactly 3 binary number can represent the maximum value 0b111 is also 7. Therefore, we can replace 3 binary numbers with an octal number.

0b11110011 = 0b 011 110 011 = 0363

In the case of octal to binary, in turn, a 3-bit binary number is used to represent the 1-bit octal number

025 = 0b 010 101 = 0b10101

Back to top 46 or 16 binary 1. Concept

Hex has two features: composed of 0~9 and A~f, a~f respectively represents 10~15; the arithmetic rule is "every 16 into one".

2. Details

Because 0~7 is included in decimal, octal, and Hex, in order to distinguish between hexadecimal numbers, you need to add a 0x or 0X in front of it. For example, 0x76 is a hexadecimal number, 076 is an octal number, and 76 is a decimal number.

3. Operations

The single digit is b+5, namely 11+5, the value is full 16, so 10 digits to enter one. Therefore, in hex, B + 5 = 0x10. 0x10 means "16" in decimal, not "ten".

4.16 Binary Decimal

The base of hexadecimal is 16,f represents 15 in decimal, so 0x2f is calculated as 47 in decimal

0x2F = 15 * 160 + 2 * 161 = 15 * 1 + 2 * 16 = 47

5. Binary Turn hex

A hexadecimal number can represent a maximum value of 15, exactly 4 binary numbers can represent the maximum value 0b1111 is also 15. Therefore, we can replace 4 binary numbers with a hexadecimal number.

0b11110011 = 0b 1111 0011 = 0xf3

In the case of hexadecimal to binary, in turn, a 4-bit binary number is used to represent the 1-bit hexadecimal number

0x25 = 0b 0010 0101=0b100101

Back to top five, summary of the system

1. The 4 representations of an integer "12" are as follows:

Decimal: 12

Binary: 0b1100

Octal: 014

Hex: 0xC

2. You can actually use the calculator software in your Mac to test the conversion between the binaries.

Set the computer's display mode to "programmer"

First select 10 binary, then enter 12, the bottom shows the binary number is 1100, here omit the front 0b

Select octal, shown as 14, this omits the first 0

Select hexadecimal, display as 0xC

Back to top six, variable and binary

1. Learn the use of variables in the previous lecture, briefly review

1 int Main () 2 {3 int a = 10;4 return 0;5}

A variable A is defined in line 3rd, which stores the decimal integer 10. In fact, this variable a is stored in memory as a binary number, and the binary form of 10 is 1010.

2. In addition to decimal integers, you can also assign other binary integers to integer variables

1 int Main () 2 {3 int a = 0b110;//decimal Number: 6 4 5 int b = 021;//decimal number: 6 7 int c = 12;//decimal number: 8
9 int d = 0x1D;//decimal number: 2910 return 0;12}

In the above code, 4 different binary values are assigned to different integer variables. The corresponding decimal value is already written in the comment on the right. They are eventually stored in memory in binary form.

Back to top Vii. simple use of printf 1. Output integer variables with printf

The variables are assigned a number of integers, what is the decimal form of these integers? We can convert it ourselves, but always to calculate, too troublesome, we are programmers, should use the program to help us figure out. You have already learned printf ("Hello World"); the function of the statement is to output Hello world as a string of content on the screen. We can actually use printf to output a variable to the screen to see how much the value of the variable is.

This printf seems to be what you put into its parentheses, it will output something on the screen, but the use of printf is fastidious, as the following notation is wrong:

1 #include <stdio.h> 2 3 int main () 4 {5 int a = 0x1D; 6 7 printf (a); 8 9 return 0;10}

Some may wonder why it is sometimes necessary to include <stdio.h>, and sometimes do not need # include <STDIO.H> This is not a detailed discussion, you first remember that as long as you use printf, you will add # include <stdio.h>.

In line 5th, the variable A is defined and the initial value is a hexadecimal number. In line 7th, you want to output the value of variable a by printf, but the 7th line is wrong. To use printf to output an integer variable, you must first describe the format of the output, such as the output in decimal format or in octal format?

The following wording is correct:

1 #include <stdio.h> 2 3 int main () 4 {5 int a = 0x1D; 6 7 printf ("Variable A has a value of%d", a); 8 9 Return 0;10}

Take a look at line 7th, where the double-quoted content represents what you want to output to the screen, but does not directly output "variable A's value%d" to the screen. %d is a format character that is meant to be output with the value of the right variable a instead of%d, and output in decimal format. Plainly, the format character is used to control the output format.

The result of the program running on the terminal is:

It can be found that the output of "variable a value is", the 0x1d decimal value is indeed 29, indicating that the output is correct. But this 29 with the back of the English is connected, very ugly, at this time we can add a \ n after%d for carriage return line.

1 #include <stdio.h> 2 3 int main () 4 {5 int a = 0x1D; 6 7 printf ("Variable A has a value of%d\n", a); 8 9 ret Urn 0;10}

Note the change in line 5th: Add a \ n after%d, indicating that the variable A is output in decimal format and the carriage return is followed by a newline.

The result of this operation is:

So you can obviously see this 29.

2.PRINTF supported format characters

Apart from the%d,printf also supports a number of format characters, as shown in the table below (red means commonly used), the contents of this table do not go to rote, use the time to come back to check the information can be

The following is a simple demonstration of the use of%x, which functions to output integers in 16 binary form

1 #include <stdio.h> 2 3 int main () 4 {5 int a = n; 6 7 printf ("%x\n", a); 8 9 return 0;10}

In line 5th, the variable A is defined, the decimal integer 17 is stored, and in line 7th the variable A is output in 16, and the result is:

It can be found that the decimal integer 17 output in 16 binary form is 11, which is correct.

3.PRINTF can output multiple values simultaneously

Look at the code below and use printf to output multiple values simultaneously

1 #include <stdio.h> 2 3 int main () 4 {5 int age = N; 6 7 int no = ten; 8 9 printf ("Age =% D, no =%d\n ", age, no); return 0;12}

Notice the 9th line, the left double quotation marks 2%d,age in place of the first%d for the output, the value of no will replace the second%d output, and are all in decimal form output. Output Result:

4.PRINTF can also output constants

1 #include <stdio.h>2 3 int main () 4 {5 6 printf ("Output constant is%d\n", one); 7 8 return 0;9}

Note Line 6th, on the right, is an integer constant of 11, which will be output in place of%d. Output Result:

In a half-day, it seems that there is no mention of what printf is, and this will be discussed in detail later, and it has other complex uses.

"0 Basic Learning iOS development" "02-c language" 05-binary