当前位置:网站首页>C language integer value range - the problem of more than one negative number

C language integer value range - the problem of more than one negative number

2022-06-22 03:35:00 A rainy spring night

(Owed by: Happy rain in spring night http://blog.csdn.net/chunyexiyu)

Integer range , A negative number is one more , I wonder if everyone is curious ?

Take the single byte as the discussion point , Discuss it. :

BYTE/char The value range of type is :[-128, +127], Why -128, Why not -127?

Around this problem :

We know BYTE/char Type is 8 Bit , The highest bit is the sign bit , Then the rest 7 Yes. ;

7 What can bits express , from 0b0000000 To 0b1111111, Can express 0-127 The scope of the ;

Symbol bit 0 when , Express positive numbers ,0-127 The scope of just expresses ;

Symbol bit 1 when , Express negative numbers ,0-127 The scope of the , To express -0 To -127 Do you ?0 This number has been corrected 0 Expressed , that -0 What shall I do? ? Also to express 0 Do you ?

With this problem , We need to solve this problem ,-0 To -127 The scope of the , The actual number of values is 128 Species value , If all maps to negative numbers , Indeed, it is mapped to -1 To -128 The scope of ; As consumed in bits C Language , I won't waste this 1/256 Of value taking opportunities .

Select the mapping algorithm , Even if it comes to the complement head , Physical minimum 0b10000000 That is to say -0 To express -128, Physical maximum 0b11111111 To express the minimum -1, The mapping process produces .

alike short Range /int Range /int64 This is the case with scope , This will solve the problem +0/-0 Awkward expression .

At the same time, it has created c/c++/java/c# These languages , Range of shaping variables , The negative range is greater than a value in the positive range . Because of the complement mapping , Thus, the physical -0 Is mapped to the limit value on the range of negative numbers .

(Owed by: Happy rain in spring night http://blog.csdn.net/chunyexiyu)

原网站

版权声明
本文为[A rainy spring night]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220315371044.html