C Language Blog (5) How does the programming base understand octal and hexadecimal?

Source: Internet
Author: User

In the C language learning programming process, in addition to the binary, programming also often use octal and hexadecimal. These are also the basic programming of teaching, to become the C-language of the great God, it must be understood clearly.


Octal has 0~7 a total of 8 numbers, the base is 8, every eight into one, borrow one when eight; hexadecimal, with a to indicate that the 12,d means that 13,e represents 14,f 15, so there are 0~f a total of 16 numbers, the base is 16, every 16 into 1, borrow 1 when 16. For example:

    • Octal 3072 = 3x83 + 0x82 + 7x81 + 2x80 = 1536 + 0 + 56 + 2 = 1594
    • Hex e3f9 = 14x163 + 3x162 + 15x161 + 9x160 = 57344 + 768 + 240 + 9 = 58361

Binary Binary
Binary , octal, decimal, hexadecimal correspondence
decimal octal hexadecimal decimal octal hexadecimal
0 0 0 0 10 1010 12 A
1 1 1 1 11 1011 13 B
2 10 2 2 12 1100 14 C
3 11 3 3 13 1101 15 D
4 100 4 4 14 1110 16 E
5 101 5 5 15 1111 17 F
6 110 6 6 16 10000 20 10
7 111 7 7 17 10001 21st 11
8 1000 10 8 18 10010 22 12
9 1001 11 9 19 10011 23 13


In C, octal usually begins with "0" (note that it is the number 0, not the letter O), such as 0307, and hexadecimal usually begins with "0x" or "0X" (case insensitive), such as 0xe27 or 0x89f.

Let's look at two examples of octal and hexadecimal addition operations.

1) octal addition: 0107 + 0274 = 0403


Figure 1: octal addition operation


2) Hex addition: 0xa2b + 0x276 = 0XCA1


Figure 2: Hexadecimal addition operation This is the basic teaching of programming for the general population, I will be in this C-language blog every time to update some of the basic programming needs of beginners next we want to introduce the conversion between the problem, please look forward to!

C Language Blog (5) How does the programming base understand octal and hexadecimal?

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.