Overflow of operation signed numbers

Source: Internet
Author: User

Overflow of operation signed numbers

In computer systems, the binary representation of numeric values mainly includes the original code, reverse code, and complement code. Generally, the highest bit is the sign bit. 0 indicates a positive number, and 1 indicates a negative number. The original, reverse, and complement codes of positive numbers are the same. The highest bitwise of the negative original code is 1, and the numerical bitwise is the binary value of the absolute value of the negative number. The reverse bitwise is 1, and the other bitwise is reversed. The complement bitwise is 1, the other bits are reversed by 1.

In a 32-bit computer, the value range of the signed variable is-27 ~ 27-1, The value range of the signed integer variable is-215 ~ 215-1If you assign a signed integer variable to a signed integer variable, the variable is truncated at a high level and the value is kept as low as eight digits.

Let's look at an example:

#include
 
  #include
  
   int main(){char a[1000];int i;for(i = 0; i < 1000; i++){a[i] = -1 - i;}printf("%d\n", strlen(a));return 0;}
  
 

Statement a [I] =-1-I; assign a signed integer variable to a variable of the signed integer type. When 0 <= I <= 127, no overflow occurs, a [I] is changed from-1 to-128. When I = 128,-1-I =-129. -The original code of 129 is:

1000 0000 0000 0000 0000 00001000 0001; the complement form is

1111 1111 1111 1111 1111 11110111 1111; in this case, when it is assigned to a [128] of the signed char type, overflow occurs, only keep the lower eight bits 0111 1111, that is, 127. With the increase of I, a [I] gradually approaches 0 from 127. When I = 254,-1-I =-255. The complement indicates: 1111 1111 1111 1111 1111 1111 0000 0001, a [254] = 1. When I = 255, a [255] = 0, which assigns the value of ASCII code 0 to the array element a [255]. Then a [I] is from-1 ~ -128, and then from 127 ~ 0.

The strlen function is used to calculate the actual length of a string. When the first null character '\ 0' is returned, strlen (a) = 255 is returned.


PS: NULL character '\ 0' (ASCII code value: 0), zero character '0' (ASCII code value: 48 ).

Sunday, January 1, August 10, 2014

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.