[底层] 为什么Integer.MIN_VALUE-1会等于Integer.MAX_VALUE

Integer.MIN_VALUE-1 = Integer.MAX_VALUE

Integer.MAX_VALUE+1 = Integer.MIN_VALUE

实际上这里是计算机底层的位运算法则问题[1]

计算机底层采用了补码来进行加减乘除的运算,好处是符号位参与运算.

举上面两个例子来说明问题。

Integer.MIN_VALUE:  $10000000000000000000000000000000$

Integer.MAX_VALUE: $01111111111111111111111111111111$

-1:                          $11111111111111111111111111111111$

一、

Integer.MIN_VALUE - 1 = Integer.MIN_VALUE + (-1)

  $10000000000000000000000000000000$

+  $11111111111111111111111111111111$

---------------------------------------------------

 $1,01111111111111111111111111111111$

舍弃最高位的进位,所以得到的就是Integer.MAX_VALUE

二、

Integer.MAX_VALUE + 1= Integer.MIN_VALUE

  $01111111111111111111111111111111$

+ $00000000000000000000000000000001$

---------------------------------------------------

  $10000000000000000000000000000000$  

除了补码之外,还有反码、移码等等。

1、反码

整数是它本身,负数是符号位不变数值位求反

2、移码

主要用于浮点数的表示当中的阶码

相应的补码符号位求反就是移码

[1] http://wenku.baidu.com/link?url=8ques5GUmTWrFybpKKFQG_bstr-7A4EdLatGIqcFMaL9xtDBgrSrO0GHEvQ5MZgld4u1leMs04wvqsYnnQ9CS_ryY1gu7YyW0n5pdni8_Qy

[2] http://blog.csdn.net/meng4411yu/article/details/8668625

时间: 2024-12-10 07:19:04

[底层] 为什么Integer.MIN_VALUE-1会等于Integer.MAX_VALUE的相关文章

Integer.MIN_VALUE

static int MAX_VALUE           值为 2^31-1 的常量,它表示 int 类型能够表示的最大值. static int MIN_VALUE           值为 -2^31 的常量,它表示 int 类型能够表示的最小值. 并且会有 Integer.MAX_VALUE+1==Integer.MIN_VALUE

关于Double.MIN_VALUE和Integer.MIN_VALUE

Integer.MIN_VALUE自不必说,就是32位整型能存储的最小数字:0x80000000,是一个负数. 但是Double.MIN_VALUE却是一个正数: 可以看到,Double.MIN_VALUE表示的时64位双精度值能表示的最小正数. 如果需要用到Double的负无穷,可以用Double.NEGATIVE_INFINITY

LeetCode之Integer to Roman, Roman to Integer

罗马数字以前只接触过I到VIII,第一次听说罗马数字也可以表示大于8的数字.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则.Wiki了一把,又参考了其它的文档,总结如下: 罗马数字规则: 1, 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000). 罗马数字中没有"0". 2, 重复次数:一个罗马数字最多重复3次. 3, 右加左减: 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字. 在较大的罗马数字的左边记上

Integer to Roman & Roman to Integer

Integer to Roman Given an integer, convert it to a roman numeral. The number is guaranteed to be within the range from 1 to 3999. Clarification What is Roman Numeral? https://en.wikipedia.org/wiki/Roman_numerals https://zh.wikipedia.org/wiki/%E7%BD%9

Integer.valueof(String s)和Integer.parseInt(String s)的具体区别是什么?

Integer.valueof(String s)是将一个包装类是将一个实际值为数字的变量先转成string型再将它转成Integer型的包装类对象(相当于转成了int的对象)这样转完的对象就具有方法和属性了.而Integer.parseInt(String s)只是将是数字的字符串转成数字,注意他返回的是int型变量不具备方法和属性. 设有下面两个赋值语句:a=Integer.parseInt(“123”);b=Integer.valueOf(“123”).intValue();下述说法正确的

integer与int区别以及integer.values()方法详解

声明:本文为博主转载文章,原文地址见文末. 知识点1:integer和int的区别 /* * int是java提供的8种原始数据类型之一.Java为每个原始类型提供了封装类,Integer是java为int提供的封装类.int的默认值为0, * 而Integer的默认值为null * ,即Integer可以区分出未赋值和值为0的区别,int则无法表达出未赋值的情况,例如,要想表达出没有参加考试和考试成绩为0的区别 * ,则只能使用Integer * .在JSP开发中,Integer的默认为nul

Integer.parseInt(String s) 和 Integer.valueOf(String s) 的区别

通过查看java.lang.Integer的源码可以发现, 它们最终调用的都是 /** * Parses the string argument as a signed integer in the radix * specified by the second argument. The characters in the string * must all be digits of the specified radix (as determined by * whether {@link

Integer a = 128, Integer b = 128, a==b ; Integer c = 1 , integer d =1 , c==d

Integer a = 128; Integer b = 128; System.out.println(a==b); Integer c = 1; Integer d = 1; System.out.println(c==d); 执行结果:false true 解释原因: 类似String一样 对于这样直接赋值的Integer 当值的范围在 [-128,127] 时候 会不生成新的对象 直接把缓存中的对对象拿来用 Integer a=128;Integer b=128 这样的话就是false了

LeetCode12~14 Integer to Roman/Roman to Integer/Longest Common Prefix

一:Integer to Roman 题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 链接:https://leetcode.com/problems/integer-to-roman/ 分析:此题关键是确定罗马数字如何表示,比如4并不是IIII而是IV,9并不是VIIII而是IX, 通过不断的取出最高位和余下的得到结果