Detailed 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 and most often used. In addition, the introduction of 8 binary and 36 binary, in which 36 binary is encountered in the actual project projects. (This article has been selected from the C + + Learning Guide.) Shaofa. Appendix "2 Binary, 10 binary, 16 binary")speak 2-in, 10-binary, 16-Step video tutorials, click to view

Rights Statement : The author has all the rights to this book.

The author authorizes no matter who can freely reprint the content published on this site, but must abide by the following restrictions when reproduced: ① reprint must be reproduced in full text. No matter what changes, must include the "rights statement" and "official website address" ② Limited to the network reprint, that is, finally published on the network. Any non-compliance with the above two of the reprint act as a tort.

Unless I agree. No person shall use the content of this site for any other purpose whatsoever.
website Address: http://www.afanihao.cn/message please go to http://www.afanihao.cn/kbase/

1.1 Example One

Give a piece of code first, and ask the reader to compile it now and execute it again. Observation 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 have not compiled yet, please stop reading this book immediately, as this book is not intended for people who are too lazy. For all the code and demo samples, please make sure that you do not copy them by hand. If you are copying the past, please burn the book right away. 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, just a different way of writing.

Either way you can.

1.2 Example Two

Give me another piece of code, please compile and execute it immediately:

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


Its execution results are:

Yes, for the same integer a. When it is displayed as a decimal. Display as 65, when displayed as hexadecimal as 41, displays a when displayed as a character.

1.3 Example Three

Let's say the example above gives you an impression that's not deep enough. Try another example:

#include <stdio.h>void main () {       //The values of the variables A and B below are the same       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

The content of this section is very important for understanding this chapter and even the whole book. Assuming you understand, then the so-called problem of the system is no longer a problem for you.

First, the emphasis that has been made clear in chapter three. Each of these variables has a memory address. For char-type variables. Accounted 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 the memory of the computer. How the inverted bottom is stored. We start with the most simple number. Like what. Defines a variable, char a = 13, which 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 expression of 1, how to express 0, the reader does not need to care.

You can simply think of a bit as a corresponding "switch". 1 means open, 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. The bitwise physical representation of a byte is 0000 1101.

Are you clear? The so-called 10-binary or 16-binary is for humans. Not the same argument.

But for a computer, this value is stored. is to use 8 bits (the corresponding 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 it can be displayed in 10 binary. can also be displayed as 16-in-binary.

To you is the pupil level, that well, as the program ape I just good to press 10 to show you the binary.

printf ("%d", a);

Suppose you have read the book by chance, well. I give it a 16-in-binary display:

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.

In fact, as the author has repeatedly emphasized. Programming does not require too high a level of mathematics, almost no need 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? The following is a demonstration example: (The following is not code, is the hand calculation method in 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

Can see. For 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

It can be seen that for 2 binary, the weight on each number is 2n-1 (i.e. 2 n-1)

1.6 How to print 2 binary

In printf parameters, you can print in 10 by using the control character%d. Use%x to print in 16. So, did you press 2 to print it?

It is a pity that 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 project practice. This method of use is of no value. Similar to the previous. 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 also the same, the weight of each number is 36n-1 (36 of the n-1).

Like what. (not the code below.) Is the hand count method on the draft paper)

2Z = 36 * 2 + 35 = 107

In fact, 36 binary is a very useful knowledge in project practice. Example. The same 4 characters are used to represent numbers. Using 10 binary can represent 0000~9999, using 16 binary can represent 0000~FFFF, using 36 binary can represent 0000~zzzz.

The ability to use 36 binary notation is strongest.

Detailed 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.