C-language octal 16 binary code inverse code with bitwise complement

Source: Internet
Author: User
Tags bitwise numeric modulus

How to express 8 and 16 in C language

The three types of input supported by the C language itself are:
1. Decimal. Like 20,457;
2.16 binary, beginning with 0x. such as 0x7a;
3. Eight binary, beginning with 0. Like 05,0237.

So C language does not have binary input, the most available function to achieve.

the expression method of octal number

C + + regulations, a number if you want to indicate that it uses octal, you must precede it with a 0 (number 0), such as: 123 is decimal, but 0123 means the use of octal. This is the octal number in C, C + + expression method.

C and C + + do not provide a binary number expression method

Now, for the same number, like 100, we can use the normal 10 in code, for example, when a variable is initialized:

int a = 100;

We can also write this:

int a = 0144; 0144 is the octal 100;


The use of octal numbers in escape characters

We have learned to use an escape character '/' plus a special letter to denote a character's method, such as: ' \ n ' means newline (line), and ' \ t ' represents the tab character, ' \ ' is a single quotation mark. Today we learned another way to use the escape character: the escape character ' \ ' followed by a octal number that represents the character that the ASCII code equals the value.

For example, check the ASCII code table, we find the question mark character (?) The ASCII value is 63, then we can convert it to octal value: 77, and then use ' \77 ' to denote '? '. Since it is octal, it should have been written as ' \077 ', but because C + + regulations do not allow the use of slashes plus 10 to represent characters, so here's 0 can not write.

For example:

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

The output result is:

?

?

16 representation: number sequence beginning with 0 x or 0x (number 0)

Like 24 is 0x018

In addition, a=10,b=11,c=12,d=13,e=14,f=15

For example

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

Result is

24

24

14

original code, inverse code and complement

Overview
Within a computer, there are 3 representations of signed numbers: The original code, the inverse code, and the complement.

In the computer, the data is stored in the form of complement, so the complement in C language teaching has a relatively important position, and the interpretation of the complement must involve the original code, the inverse code.

detailed explanation
The original code is the binary fixed-point notation, that is, the highest bit for the symbol bit, "0" is positive, "1" means negative, the remaining bits represent the size of the value.

The anti-code notation stipulates that the inverse code of a positive number is the same as its original code; the inverse of a negative number is a bitwise counter to its original code, except for the sign bit.

The complement notation stipulates that the complement of a positive number is the same as its original code; the complement of a negative number is at the bottom of its inverse code plus 1.

the representation method of the original code, the inverse code and the complement

Fixed-point integer Representation method
Original code
A notation that directly adds a sign bit before a numerical value.

For example: sign bit numeric bit

[+7] Original = 0 0000111 B

[-7] original = 1 0000111 B

Note:

A. Number 0 of the original code has two forms:

[+0] Original = 00000000B

[-0] Original = 10000000B

B. Representation range for 8-bit binary source code: -127~+127


Fixed-point Decimal Representation method

Anti-code
Positive number: The inverse code of a positive number is the same as the original code.

Negative number: Negative inverse code, symbol bit is "1", the numeric value part is reversed by bit.

For example: sign bit numeric bit

[+7] anti = 0 0000111 B

[-7] anti = 1 1111000 B

Note:

A. The inverse code of number 0 also has two forms, namely

[+0] Anti-=00000000b

[-0] anti-=11111111b

B. 8-bit binary trans-code representation range: -127~+127

complement
1 model concept: a unit of measurement called a modulus or modulus.

For example, a clock is a loop of counting in 12, which is modulo 12. On the clock, the position of the hour hand is the same as the integer digit of 12 or minus (redial) 12.

For a circulatory system with a modulus of 12, the effect of adding 2 and minus 10 is the same; therefore, in the 12-mode system, all minus 10 operations can be replaced by plus and two, which translates the subtraction problem into an additive problem (note: The computer's hardware structure is only adder, So most of the operations must eventually be converted to addition.

10 and 2 are complementary to modulo 12.

Similarly, the computer's operational components and registers have a certain length limit (assuming the word length is 8), so its operation is also a modular operation. When the counter is full of 8 digits or 256 digits, an overflow occurs and the count starts from scratch. The amount of overflow is the model of the counter, obviously, 8-bit binary number, its modulus is 2^8=256. In the calculation, two complementary numbers are called "complement".

2) The expression of the complement:

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

Negative number: The complement of negative numbers is the sign bit is "1". And, this "1" is both a sign bit and a numeric bit. The value portion is reversed and then at the bottom (lowest position) plus 1. Which is "Counter code +1".

For example: sign bit numeric bit

[+7] complement = 0 0000111 B

[-7] complement = 1 1111001 B

Complement in the microcomputer is an important form of coding, please note:

A. After using the complement, the subtraction operation can be easily converted into an addition operation, and the operation process is simplified.

The complement of a positive number is the true value of the numbers it represents, while the numeric part of the complement of a negative number is not the truth of the numbers it represents.

Using the complement, the result is still the complement.

B. Unlike the original code, the inverse code, the value of 0 of the complement is only one, namely

[0] complement =00000000b.

If the word length is 8 bits, the range of the complement is -128~+127, and the result should not exceed the range of the number that can be expressed in the complement.

conversion between the original code, the inverse code and the complement
Because the original code, the complement, the inverse code means all the same, do not need to convert.

In this case, only negative numbers are analyzed.
(1) The original code is known, and the complement is obtained.
Example: The original code of a certain number x is 10110100B, try to find the complement and inverse code of X

Solution: by [x] original =10110100b known, X is negative. When the code is reversed, the symbol bit is unchanged, the numerical part is reversed by the bit, and the complement of the code is added 1.

1 0 1 1 0 1 0 0 Original code

1 1 0 0 1 0 1 1 Anti-code, symbol bit unchanged, numeric position reversed

1 +1

1 1 0 0 1 1 0 0 complement

Therefore: [X] complement =11001100b,[x] anti-=11001011b.
(2) Known complement, seek the original code.
Analysis: In accordance with the inverse process of the complement of negative numbers, the numerical part should be the lowest minus 1, and then take the counter. But for the binary number, the first minus 1 after the reverse and first take the reverse and then add 1 results are the same, it can still be used to take the counter plus 1 have methods.

Example: The complement 11101110B of a certain number x is known, try to find its original code.

Solution: by [x] complement =11101110b know, X is negative.

1 1 1 0 1 1 1 0 complement

1 1 1 0 1 1 0 1 counter code (minus 1)

1 0 0 1 0 0 1 0 Original code (symbol bit unchanged, other counter)


Overflow problem in symbolic number operation

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

The complement is also: 1111 1110.
Complement: The complement of a positive number is its inverse code (also its original code), the complement of the negative number is its original code by the counter, and at the bottom plus one, so
1 's complement is also 0000 0001.
And-1 of the complement is 1111 1110, then the bottom plus 1, into 1111 1111
32-bit machine-1 of the complement is 1111 1111 1111 1111 1111 1111 1111 1111

Conversion into 16 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);
}

Results:

1
Ffffffff
1
Ffffffff


typedef unsigned short int Uint16;
Uint16 a=10;
Uint16 b=20;
So Uint16 c= a-b get 65526,
Short int D=a-b get-10,


C Language Bitwise counter operator ~

Note: The C-language bitwise negation operation is also reversed for the symbol bit

What is the result of the C-language ~12?

The short type is example 12 with a binary of 0000 1100

Take the opposite. 1111 0011

And this number is expressed as-13 with a signed shape (%d printed out)

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

The absolute value of a negative number equals: Reverse + 1

1111 0011 reverse plus 1 to 0000 1101 = 13

So for-13

The highest bit is the symbol bit

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

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

Source code compilation Environment vs.net2005, 32-bit machine

The. netclr stipulates that the integer variable defaults to the int type, which means that the 7 in memory occupies 4 bytes.

00000000 00000000 00000000 00000111 (7)

Bitwise NEGATION OPERATION
11111111 11111111 11111111 11111000
printf ("%x\n", ~7)//hexadecimal output: FFFFFFF8
printf ("%d\n", ~7)//Decimal Output:-8

The reason-8 is the result overflow, such as

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

As a result, the output of 8 is greater than the range of int



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.