Range issues for signed integer types

Source: Internet
Author: User

A range of signed integer types:

Integer type: Byte,short,int,long

Byte:8 bit -128--->127

Short 16-bit -32768--->32767

int 32-bit -2147483648-->2147483647

Long 64-bit -9223372036854775808--9223372036854775807

Why is such a range:

Original code: Say a number converted to binary, plus the symbol bit is the original code (0 for integers, 1 for negative numbers)

Anti-code, the inverse of the integer is its original code, negative anti-code is in addition to the symbol, each bit counter.

Complement: The complement of an integer is its original code, the complement of negative is its inverse code plus one.

Example: The original code for a single-byte 5,-5 is: 0000 0101 1000 0101

5,-5:0000 0101 1111 1010

5,-5 's complement is: 0000 0101 1111 1011

Why do computers use the complement to denote negative numbers?

Advantages:

1 prevent having "two 0". The eight-bit could have represented 256 data, and 0000 0000 and 1000 0000 represented zero if they were in the original code.

The complement of 0000 0000 and 1111 1111 means zero, which is a bit odd. But in the form of a complement? Only 0000 0000 This representation method.

2 Convenient computer operation:

The calculation of the complement:

The complement method of 2 positive number is the same as the complement method of negative number.

-5 of the complement of a 5-plus complement

-5, 5:1111 1011, 0000 0100, 0000 0101

The complement of 5 is one-5-plus

5, -5:0000 0101, 1111 1010, 1111 1011

2 convenient operation, variable subtraction for addition.

3 .....

These are just a few things to know.

Return to the original question, why the 8-bit byte data range is 128 to 127, we know this knowledge point.

8-complement Maximum: 0111 1111 (127) min.: 1000 0000 (-128)

16-bit, 32-bit, 64-bit similar.

Therefore, the N-bit type represents a signed number range of-(2^ (n-1))->2^ (n-1)-1

I am a rookie, welcome you to correct me.


This article is from the "Best Scenery" blog, so be sure to keep this source http://runrun.blog.51cto.com/9754749/1710157

Range issues for signed integer types

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.