Value range of tinyint in MySQL

Source: Internet
Author: User

Http://indian.blog.163.com/blog/static/108815820056403180/

 

In MySQL, the value range of tinyint is-128 to 127. The unsigned range is 0 to 255 (see the official MySQL
5.1 reference manual http://dev.mysql.com/doc/refman/5.1/zh/column-types.html#numeric-types ).

 

Tinyint occupies 1 byte of storage space, that is, 8 bits ). So how does tinyint value range come from? Let's first look at the unsigned situation. The unsigned minimum value means that all eight bits are 0, and the value is 0 in decimal format. Therefore, the minimum value of the unsigned tinyint is 0. the maximum unsigned value means that all 8 bits are 255, which is in decimal format. this is easy to understand.

 

How does a signed tinyint value range come from? In a computer, the highest bit is used as the symbol. 0 indicates positive, 1 indicates negative, and the rest indicates numerical values. The minimum value of a signed 8-bit is

 

1 1 1 1 1 1 1 1 1 =-127

Negative value

Maximum value:

0 1 1 1 1 1 1 1 = + 127

Positive Value

 

How can the minimum signed value be-127 instead of-128? This is the key to this article, in the computer, the negative value is to use the complement code (positive code, back code, complement the concept of code see the http://indian.blog.163.com/blog/static/1088158200610942745817)

 

Why is the minimum value of a signed tinyint-128? Although "-0" is also "0", the "-0" complement code is different from "+ 0" according to the positive, reverse, and complement system, in this case, two supplementary codes represent a value. In order to match the complement code with numbers one by one, "0" is always represented by "+ 0. At the same time, in order to make full use of resources, the original "-0" complement code should be set to represent-128.

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.