"In-depth understanding of computer systems" 2.2 Integer Representation

Source: Internet
Author: User
Tags byte sizes

Integer data type

Machines and compilers with different word lengths in C are assigned different byte sizes.
The long type is the only machine-related.

Typical range of integer data types for the C language on 32-bit machines
Negative value range is 1 larger than positive

C Data Type Minimum Maximum
Char ? 128 127
unsigned char 0 255
short [int] ? 32,768 32,767
unsigned short [int] 0 65,535
Int ? 2,147,483,648 2,147,483,647
unsigned [int] 0 4,294,967,295
long [int] ? 2,147,483,648 2,147,483,647
unsigned long [int] 0 4,294,967,295
Long Long [int] ? 9,223,372,036,854,775,808 9,223,372,036,854,775,807
unsigned long long [int] 0 18,446,744,073,709,551,615

Typical range of integer data types for the C language on 64-bit machines
Negative value range is 1 larger than positive

C Data Type Minimum Maximum
Char ? 128 127
unsigned char 0 255
short [int] ? 32,768 32,767
unsigned short [int] 0 65,535
Int ? 2,147,483,648 2,147,483,647
unsigned [int] 0 4,294,967,295
long [int] ? 9,223,372,036,854,775,808 9,223,372,036,854,775,807
unsigned long [int] 0 18,446,744,073,709,551,615
Long Long [int] ? 9,223,372,036,854,775,808 9,223,372,036,854,775,807
unsigned long long [int] 0 18,446,744,073,709,551,615
Guaranteed range of values for integer data types in the

C language
range of values is symmetric

0
C data type Minimum Maximum
Char ? 127
unsigned char 0 255
short [int] ? 32,767 32,767
unsigned short [int] 0 65,535
int ? 32,767 32,767
unsigned [int] 65,535
long [int] ? 2,147,483,647 2,147,483,647
unsigned long [int] 0 4,294,967,295
Long Long [ int] ? 9,223,372,036,854,775,807 9,223,372,036,854,775,807
unsigned lon g long [int] 0 18,446,744,073,709,551,615

Added: c/C + + supports signed and unsigned, Java only supports signed

Encoding for unsigned numbers


Use B2 U w (x) function to turn a bit vector into a 10 binary number

The maximum number of unsigned values is:

So the unsigned value range is

Complement code

The computer indicates that negative numbers are usually in complement form


Use B2 T w (x) Replace a signed binary with a 10 binary

A binary in addition to the symbol bit to reverse the remaining bits, and then at the end of +1, get the complement
For example: 1011

    1. In addition to the symbol bit inversion: 1100
    2. End +1:1101
    3. The number of symbols determines that the number is negative, and the three bits determines the absolute value of the number is 5.
    4. So this number is--5.
      The complement code values range from:

Since 0 is a non-plural, the number of positive numbers is less a

The advantages of the number of machines in the complement show:

The original code is simple and suitable for multiplication operation, but it is more complicated to add and subtract the number represented by the original code.
Complement, subtraction operation can be achieved by addition, for example [x-y] complement = [+[-y] complement,
Moreover, the number of sign bits can also participate in the operation, to facilitate the calculation of the results of positive and negative and whether overflow judgment.
Therefore, most of the computers use the complement to add and subtract and multiplication operations.
(not only integers, decimals can also be expressed in complement)

Determining the size of an integer type

It is important to have a certain size of performance coded by type
For example
It is important to make the data type compatible with the data type specified by the protocol.

Java requires a complement representation, and the value is the same as the typical range for the integer data type of the C language on a 64-bit machine. Single byte with Byte, no long long.

Anti-code and complement inverse code formula

Complement formula

Conversion between signed and unsigned

The C language converts short int to unsight short, changing the value without changing the bit pattern
Like what
-12345 (complement) binary: 1100 1111 1100 0111
Convert to unsigned
53191 (unsigned) binary: 1100 1111 1100 0111

Signed Conversion unsigned

Unsigned sign-changer

In general, for W-bit numbers

    • Signed and unsigned
      • Positive number unchanged
      • Negative numbers are added 2^w
    • Unsigned change-Signed
      • If the number is less than 2^ (w-1), it does not change
      • If the number is greater than 2^ (w-1), subtract 2^w

operator, if one is a signed number and one is an unsigned number, the signed number is converted to unsigned. This conversion will appear to be not intuitive in logical operations

Extends the bit representation of a number
    • Converts an unsigned number to a large range of data types, as long as 0 is added at the beginning, and this operation is called 0 expansion
    • Extend the complement number to a larger range of data types, as long as a copy of the value of the most significant bit is added earlier, that is, the highest bit is 0, add 0 to 1 and add 1.
      • For example, a 10,104-bit number expands to 6 bits, which is 111010.
    • For changing a short type number to unsigned int, change the size and then from signed to unsigned
Truncate numbers


Unsigned number if the operation becomes negative, it will be considered a large unsigned number in the computer. This feature can be a security risk for your computer. To prevent this problem, never use unsigned



From for notes (Wiz)

"In-depth understanding of computer systems" 2.2 Integer Representation

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.