The value range of TINYINT in mysql, and that of mysqltinyint

Source: Internet
Author: User

The value range of TINYINT in mysql, and that of mysqltinyint

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 point of this article. In the computer, the negative value is supplemented

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.


Mysql unsigned tinyint can store up to many Integers

In MySQL, the value range of Tinyint is-128 to 127. The unsigned range is 0 to 255.
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.
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

Mysql Data Type tinyint

This should be controlled in the code. It cannot be displayed if it is directly queried in the database. Use the code to process the query results.

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.