Detailed explanation 2 binary, 10 binary, 16 binary, 8 binary, 36 binary

Source: Internet
Author: User

This article introduces the concept of the introduction of the C + + language, mainly introduces 2 binary, 10, 16, which are the three kinds of programming must be mastered is also the most commonly used. In addition, the introduction of 8 and 36 binary, where 36 in the actual project will be encountered. (This article has been selected from the C + + learning Guide, Shaofa, appendix "2 Binary, 10 binary, 16 binary")

Rights Statement : The author has all the rights in this book. The author authorizes anyone to freely reprint the content posted on this website, but must abide by the following restrictions when reproduced: ① reprint must be reproduced in full, no modification, must include "rights statement" and "official address" ② Limited to the network reprint, that is, the final results published on the network. Any non-compliance with the above two of the reprint act as a tort. Except as permitted by me, no person shall use the content of this website for any other purpose.
website Address: http://www.afanihao.cn/message please go to http://www.afanihao.cn/kbase/

1.1 Example One

Given a piece of code, let the reader now compile and run it again, observing the results.

#include <stdio.h>void main () {       int a = ' a ';       int b = n;       int c = 0x41;       printf ("%d,%d,%d \ n", A, B, c);}

If you haven't compiled it yet, please stop reading this book immediately, because this book is not for people who are too lazy. All the code and examples, please make sure that you do not copy the user hand over. If you copy the past, please burn down the book immediately, thank you.

You will find that the output is:

Yes, the value of the variable a,b,c is exactly the same. In fact, these three types of initialization (Assignment) methods are completely equivalent, but not the same . You can use whichever method you have.

1.2 Example Two

To give another piece of code, compile and run it now:

#include <stdio.h>void main () {       char  a = n;       printf ("%d,  %02x,  %c \ n",  A,  A,  a);}


It is run as a result:

Yes, for the same integer A, when it is displayed as a decimal, it is displayed as 65, and when it is displayed as a hexadecimal, 41 is displayed as a character.

1.3 Example Three

If the above example gives you an impression that is not deep enough, try one more example:

#include <stdio.h>void main () {       //The values of the following variables A and B are the same as       int a = 0x12345678;       int b = 305419896;       printf ("%d,%d \ n", A, b);        The contents of the buf are "ABC"       char buf[4] = {0x41, 0}, ' C ';       printf ("%s \ n", buf);}


The result is:

1.4 2-binary representation of integer variables

This section is very important to understand this chapter and even the whole book, if you understand, then the so-called problem of the system is not a problem for you.

First, it has been explicitly emphasized in chapter three that each variable has a memory address. For char variables, it accounts for 1 bytes in memory. For an int variable, it occupies 4 bytes in memory. To make it easier for you to understand, let's start with char.

Then, let's look at how the bottom is stored in the computer's memory. We start with the most simple number. For example, to define a variable, char a = 13, this variable a takes up one byte of memory. In memory, a byte has 8 bits (bit), and each bit can be 1 or 0. Then the expression of a is:

0

0

0

0

1

1

0

1

That is, in physical memory, it is stored in this way. As for the physical representation of 1, how to represent 0, the reader does not need to care. You can simply assume that a bit corresponds to a "switch", 1 means open, and 0 means close. Later, we call it a bitwise representation of 0000 1101.

So, char a = 13 or char a = 0x0C, the two notation means the same: assigning a variable, corresponding to a byte, and the bitwise physical representation of the byte as 0000 1101.

Do you understand? The so-called 10-binary or 16-binary is for humans, not the same argument. But for the computer, to store this value is to use 8 bits (corresponding to 8 physical switches). This is the philosophical "form and content" relationship.

In turn, we already know that a variable A has a memory representation of 0000 1101, and we want to display its value on the console. Then you can press 10 to display, or you can press the 16 binary display.

You are a pupil level, well, as a programmer I had to press 10 to display to you.

printf ("%d", a);

If you happen to have read the book, well, I'll show you the 16-based binary:

printf ("%02x", a);

1.5 Binary Conversion

This section describes the contents of mathematics. The author's mathematical level is not high, here can only let you understand a bit. In fact, as the authors have repeatedly emphasized, programming does not require too high a level of mathematics and is almost no requirement for mathematics.

First of all, say the conversion from 16 to 10 binary. We know that 0x41 (hex) and 65 (decimal) are equivalent. So how do you manually calculate it? Here is an example: (The following is not the code, is the hand calculation method on the grass manuscript )

0x41 = 4 * 16 + 1 = 65

0x12345678 = 1 * 167 + 2 * 166 + 3*165 + 4 * 164 + 5 * 163 + 6 * 162 + 7 *161 + 8 = 305419896

As you can see, for the 16 binary, the weight on each number is 16n-1 (i.e. 16 n-1)

Then, say the conversion from 2 binary to 10 binary.

0000 1101 = 0 + 0 + 0 + 0 + 8 + 4 + 0 + 1 = 13

or Strictly:

0000 1101 = 0 + 0 + 0 + 0 + 23 + 22+ 0 + 1 = 13

As you can see, for the 2 binary, the weight on each number is 2n-1 (i.e. 2 n-1)

1.6 How to print 2 binary

In the printf parameter, the use of the control character%d can be printed in 10, using%x can be printed in 16, then, is there a 2 binary printing?

Unfortunately, printf cannot print the binary directly. To compensate for this regret, the author contributed a function that was printed by 2.

#include <stdio.h>//Convert an integer to a binary string//value: Input integer//buf: Output string//Num_of_bits: Specifies the number of bits to print void to_binary (unsigned int va Lue, char buf[], int num_of_bits) {for       (int i=0; i<num_of_bits; i++)       {              unsigned int mask = 1 << (num_o F_bits-1-i);              if (value & mask)                    buf[i] = ' 1 ';              else                    buf[i] = ' 0 ';       }       Buf[num_of_bits] = 0;} void Main () {       char buf[33];       To_binary (135, buf, 8);       printf ("%s \ n", buf);}


1.7 8 Binary

8 binary is generally not used in engineering practice, and this usage is of no value. Similar to the previous one, the 8 binary indicates that the weight of each number is 8n-1 (that is, 8 n-1). For example, 15 (octal) equals 13 (decimal).

The literal constants of octal are prefaced with 0 in the C + + language. For example, the following code,

#include <stdio.h>void main () {       int a = 015;//starts with 0 for octal       int b = 0x0D;//starts with 0x for hexadecimal       int c =;  Decimal        printf ("%d,%d,%d \ n", A, B, c);}


1.8 36 Binary

Just as the sequence 0, 1, 2, ..., 9, A, ..., f to represent the 16 binary, with the sequence 0, 1, 2, ..., 9, A, B, ..., Y, Z to represent the 36 binary. The method of operation is exactly the same, the weight of each number is 36n-1 (36 of the n-1).

For example, (the following is not code, is the hand method on the draft paper)

2Z = 36 * 2 + 35 = 107

In fact, the 36 system is a very useful knowledge in engineering practice. For example, the same 4 characters to represent numbers, using 10 binary can represent 0000~9999, using 16 binary can represent 0000~FFFF, using 36 binary can represent 0000~zzzz. Yes, the most powerful representation with 36 binary.

Detailed explanation 2 binary, 10 binary, 16 binary, 8 binary, 36 binary

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.