C language in the octal and 16 how to express, the original code, inverse code and complement, C language bitwise counter operator ~

Source: Internet
Author: User
Tags bitwise

Borrowed
How to represent octal and hexadecimal in C language

The three inputs supported by the C language are:
1. Decimal. Such as 20,457;
2. Hex, starting with 0x. For example 0x7a;
3. Octal, starting with 0. E.g. 05,0237

So C language has no binary input and can be implemented with functions at most.

Expression of octal numbers

C / C ++ stipulates that if a number is to be specified in octal, it must be preceded by a 0 (digit 0), such as: 123 is decimal, but 0123 means octal. This is how octal numbers are expressed in C and C ++.

Neither C nor C ++ provides a representation of binary numbers

Now, for the same number, such as 100, we can use ordinary decimal expressions in the code, such as when the variable is initialized:

int a = 100;

We can also write this:

int a = 0144; // 0144 is octal 100;

Be sure to remember that when using octal expressions, you must not lose the first zero. Otherwise, the computer will treat it as decimal. However, there is a place where octal numbers are used without adding 0, which is the "escape character" expression for expressing characters.
Use of octal numbers in escape characters

We have learned how to use an escape character '/' and a special letter to represent a character, such as: '\ n' represents a newline (line), '\ t' represents a Tab character, and '\' 'then Represents single quotes. Today we learned another way to use escape characters: the escape character '\' is followed by an octal number, which is used to represent a character whose ASCII code is equal to the value.

For example, looking up the ASCII code table, we find that the ASCII value of the question mark character (?) Is 63, then we can convert it to an octal value: 77, and then use '\ 77' to represent '?'. Because it is octal, it should have been written as '\ 077', but because C / C ++ rules do not allow slashes and decimal numbers to be used to represent characters, the 0 here can be omitted.

E.g:

printf ("\ 077 \ n \ 77 \ n")

The output is:

?

?

Hex representation: sequence of digits starting with 0X or 0x (digit 0)

If 24 is 0x018

In addition, A = 10, B = 11, C = 12, D = 13, E = 14, F = 15

E.g

#include <stdio.h>
main ()
{
int a = 0x018, b = 24, c = 016;
printf ("% d \ n", a);
printf ("% d \ n", b);
printf ("% d \ n", c);
}

The result is

twenty four

twenty four

14

Original, complement and complement

Overview
In the computer, there are 3 representations of signed numbers: original code, complement code and complement code.

In the computer, data is stored in the form of two's complement, so the two's complement has a more important position in the teaching of the C language, and explaining the two's complement must involve the original code and the inverse code.
Detailed explanation
The so-called original code is binary fixed-point representation, that is, the most significant bit is the sign bit, "0" means positive, "1" means negative, and the rest of the bits indicate the magnitude of the value.

The inverse notation provides that the inverse of a positive number is the same as its original code; the inverse of a negative number is the bitwise inversion of its original code, except for the sign bit.

The two's complement notation provides that the complement of a positive number is the same as its original code; the complement of a negative number is one added to the last bit of its complement.
Representation of source code, complement code and complement code
How to represent octal and hexadecimal in C language, original code, complement and complement, C language bitwise negation operator ~

Fixed-point integer representation
Original code
Representation by adding a sign bit directly before the value.

For example: sign bit value bit

[+7] Original = 0 0000111 B

[-7] Original = 1 0000111 B

note:

a. The source code of the number 0 has two forms:

[+0] Original = 00000000B

[-0] Original = 10000000B

b. The range of 8-bit binary source code: -127 ~ + 127

How to represent octal and hexadecimal in C language, original code, complement and complement, C language bitwise negation operator
Fixed-point decimal representation
Reverse code
Positive number: The inverse code of the positive number is the same as the original code.

Negative number: the negative of the negative number, the sign bit is "1", and the numerical part is bitwise reversed.

For example: sign bit value bit

[+7] Inverse = 0 0000111 B

[-7] reverse = 1 1111000 B

note:

a. The complement of the number 0 also has two forms, namely

[+0] reverse = 00000000B

[-0] reverse = 11111111B

b. The range of 8-bit binary complement code: -127 ~ + 127
Complement
1) The concept of modulo: a unit of measurement is called modulo or modulo.

For example, the clock is counted and cycled in decimal, that is, modulo 12. On the clock, the hour hand adds (positive dial) an integer of 12 or subtracts (reverse dial) an integer of 12. The position of the hour hand does not change.

For a cyclic system with a modulus of 12, the effect of adding 2 and subtracting 10 is the same; therefore, in a system of 12, any operation that subtracts 10 can be replaced by adding 2. This puts The problem of subtraction has been transformed into the problem of addition (Note: There is only an adder in the hardware structure of the computer, so most operations must be finally converted to addition).

10 and 2 are complementary to modulo 12.

Similarly, the computer's computing components and registers have a certain word length limitation (assuming the word length is 8), so its operation is also a modulo operation. When the counter is full of 8 bits, that is, 256 numbers, it will overflow and start counting from the beginning. The amount of overflow is the modulo of the counter. Obviously, for 8-bit binary numbers, the modulo is 2 ^ 8 = 256. In calculations, two complementary numbers are called "complements".

2) Complementary representation:

Positive number: The complement of a positive number is the same as the original code.

Negative numbers: The complement of negative numbers is the sign bit is "1". And this "1" is both a sign bit and a value bit. Invert the value part by bit and add 1 to the last bit (lowest bit). That is "ones complement +1".

For example: sign bit value bit

[+7] Complement = 0 0000111 B

[-7] Complement = 1 1111001 B

Complement code is an important form of coding in a microcomputer. Please note:

a. After using two's complement, the subtraction operation can be easily converted into the addition operation, and the operation process is simplified.

The complement of a positive number is the true value of the number it represents, but the numerical part of the complement of a negative number is not the true value of the number it represents.

The operation is complemented, and the result is still two's complement.

b. Unlike the original code and reverse code, there is only one complement of the value 0, that is,

[0] Complement = 00000000B.

If the word length is 8 bits, the range represented by two's complement is -128 ~ + 127; when performing the two's complement operation, care should be taken that the result obtained should not exceed the range that the two's complement can represent.
Conversion between source, complement and complement
Because the original code, complement code and reverse code of positive numbers are all expressed in the same way, no conversion is required.

Here, only the case of negative numbers is analyzed.
(1) Knowing the original code, find the complement.
Example: Knowing that the original code of a certain number X is 10110100B, try to find the complement and complement of X

Solution: From [X] Original = 10110100B, X is negative. When the inverse code is obtained, the sign bit is unchanged, and the numerical part is negated. When the complement is obtained, the last bit of the inverse code is increased by 1.

1 0 1 1 0 1 0 0 original code

1 1 0 0 1 0 1 1 Inverse code, the sign bit is unchanged, and the value bit is inverted

1 +1

1 1 0 0 1 1 0 0's complement

Therefore: [X] complement = 11001100B, [X] reverse = 11001011B.
(2) If the complement is known, find the original code.
Analysis: According to the inverse process of finding the complement of negative numbers, the numerical part should be the lowest bit minus 1, and then inverted. However, for binary numbers, the result obtained by subtracting 1 and then reversing is the same as that of adding 1 first, so you can still use the method of reversing and adding 1.

Example: Know the complement number 11101110B of a certain number X and try to find its original code.

Solution: Know from [X] complement = 11101110B, X is negative.

1 1 1 0 1 1 1 0's complement

1 1 1 0 1 1 0 1 Inverse (the sign bit is unchanged, the value bit is inverted and added by 1)

1 0 0 1 0 0 1 0 Original code (the sign bit is unchanged, the value bit is inverted) 1.3.2 Overflow problem in signed number operation

For example, on a 32-bit machine, the original code of 1 is (hexadecimal) 0000 0001, so its reverse code is
1111 1110.

The complement is also 1111 1110.
Complement: The complement of a positive number is its complement (also its original code), and the complement of a negative number is its original code being bitwise inverted, and one is added to the last bit, so
1's complement is also 0000 0001
The complement of -1 is 1111 1110 and the last digit is increased by 1, which becomes 1111 1111.
That is, the complement of -1 on a 32-bit machine is 1111 1111 1111 1111 1111 1111 1111 1111

Converted to hexadecimal is ffff ffff

See the following example:

#include <stdio.h>
main ()
{
int a = 1, b = -1;
printf ("% x \ n", a);
printf ("% x \ n", b);
printf ("% X \ n", a);
printf ("% X \ n", b);
}

result:

1
ffffffff
1
FFFFFFFF

C language bitwise negation operator ~

Note: The bitwise negation of C language is also negated for the sign bit: eg

C ~ 12 What is the result of bitwise negation?

The short type is an example. The binary number is 0000 1100.

Reverse is done. 1111 0011

And this number is represented by a signed integer (printed by% d) as -13

------------------------

The absolute value of a negative number is equal to: negated + 1

1111 0011 Invert and add 1 to 0000 1101 = 13

So -13

Most significant bit is the sign bit

-----------------------------------

printf ("% x,% d \ n", ~ 7, ~ 7); // Output: fffffff8, -8

Source compilation environment vs.net2005, 32-bit machine

//.NETCLR stipulates that integer variables default to int type, which means that 7 here occupies 4 bytes in memory.

00000000 00000000 00000000 00000111 (7)

Bitwise negation
11111111 11111111 11111111 11111000
printf ("% x \ n", ~ 7) // Hexadecimal output: fffffff8
printf ("% d \ n", ~ 7) // Decimal output: -8

The reason why -8 appears is the result overflow, such as

printf ("% u \ n", ~ 7) // Decimal output: 4294967288

So the output is -8 because the range of int is exceeded

For example: the following two positive numbers add up to a negative number.
1) (+72) + (+ 98) =.
0 1 0 0 1 0 0 0 B +72
+ 0 1 1 0 0 0 1 0 B +98
1 0 1 0 1 0 1 0 B -86

Two's truth
Example: -65

Original code: 11000001

Inverse code: 10111110

Complement: 10111111
If you directly convert 10111111 to decimal, the result is not -65, but 191.

So how do you get its true value?

Just follow the reverse process of complement:

If you want to get the true value of a negative binary number, you just need to subtract 1 first, and then reverse each bit (excluding the sign bit).

Binary value: 10111111 (-65's complement)
Less 1: 10111110

Invert everyone: 11000001
This becomes the result

~ -2 results in 1

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.