2013年9月28日星期六

Why 127 +1 is equal to -128

the java byte data range is -128 to 127 ; Why -128-1 will be equal to 127 ; 127 +1 and equal to -128 of it ? Counted in the end how ah ?
------ Solution ---------------------------------------- ----
byte length is 8 bits, the leftmost bit is the sign bit
127 binary : 01111111
-1 binary : 1111 1111 , plus an all-zero ,
-2 Binary : 1111 1110
......
-128 binary : 10000000

thus 127 +1 = -128
------ Solution --------------------------- -----------------
java byte type for the calculation will be converted to an int and then calculate .
example
byte b = 127 + 1 ;/ / write error : Type mismatch: cannot convert from int to byte
so you must cast :
byte b = (byte) (127 + 1);

So you can understand why it is -128 of :
int type 127 in the computer 's binary:
0000 0000 0000 0000 0000 0000 0111 1111
plus 1 becomes:
0000 0000 0000 0000 0000 0000 1000 0000
namely 128
interception after coercion becomes low 8
1000 0000
This is the complement of byte type -128 .
------ Solution ---------------------------------------- ----
hope you're using win7, open the attachment inside the calculator. View > Programmers

following number keys on the left , the decimal and byte
input 127 , you can see the bottom of the binary numbers
0111 1111 +1
Then , you will see a normal into a binary
1000 0000,
but the display is -128

because the most significant bit is the sign bit
------ Solution ------------------------------ --------------
data overflow , byte a byte 8 to only save , 256 numbers , byte from -128 to +127 ,
biggest plus > a few will be data overflow , the smallest minus > 1 will overflow.
127 +1 = -128;
127 +2 = - 127;
127 +3 = -126;

-128 - 1 = 127;
Not only is byte type , short, int, long, float, double data will overflow . In fact, very good 5th floor of the analysis .
------ For reference only -------------------------------------- -
overflow ! Maximum int + 1 = smallest int
------ For reference only ----------------------- ----------------
this is the overflow of a binary addition and subtraction problems
------ For reference only ---------- -----------------------------
support

------ For reference only ---------------------------------- -----
upstairs talking to is very detailed , byte byte representing 8 , 127 +1 to overflow , computer operations, in order to complement the form of running , the front one is the sign bit , so it becomes -128 a
------ For reference only ----------------------------- ----------
+1
------ For reference only ----------------------- ----------------
first statement is that you are sure of type byte , then byte length is 8 , the leftmost bit is the sign bit , 0 is a positive number , a negative
127:0111 1111
0: 0000 0001
sum : 10000000
then to understand that the first one is a negative, complement his abs = +1 , which is 01111111 +0000 0001 128 , so the final result is -128
---- - For reference only ---------------------------------------
above you says is the original code in addition and subtraction , the computer calculations are carried out between the original code , or twos complement arithmetic between ah ? ? Thank you
------ For reference only ------------------------------------ ---
computer composition principle did not learn
------ For reference only --------------------------- ------------

computer where is complement
you carefully understand the original code calculations and by complement calculations are the same, because the last stored in the computer are the complement.
------ For reference only -------------------------------------- -
127 binary : 01111111

-128 binary : 1000 0000
------ For reference only -------------------------- -------------
127 +1 = -128
This problem has already said very clearly the fifth floor

As for why -128-1 = 127
reasons:
-128:1000 0000
1:0000 0001
As for the concept of variable compensation , go check it
So:
-128 +1 Variable compensation :
1000 0000
1111 1111
Results:
10111 1111
take the top eight 01111111 = 127

------ For reference only ---------------------------------- -----
overflow problem ! Upstairs is very clear !
------ For reference only -------------------------------------- -
public class Demobyte {

/ **
* @ param args
* /
public static void main (String [] args) {
byte a = 127;
byte b = 1;
byte c = (byte) (b + a) ;/ / -128
/ / byte operand of type float and double if not yo inside arithmetic operations are of type int , then then cast into a byte type
System.out.println (c);
byte d = -1;
byte e = -128;
byte f = (byte) (d + e) ​​;/ / 127
System.out.println (f);
}

}


upstairs that's good . . is this if there is no float and double is converted to int op . . reckon after cast to byte! !
------ For reference only -------------------------------------- -

Dude , that can help explain -127-128 do ? Why result will be a
-127: 1111 1111
+
-128: 1000 0000
If If truncation , then, is not
= 127 0111 1111
but the result is one yet. So if I understand

-128 1000 0000 minus 127
-
127 0111 1111
= 1 0000 0001
this case is a the trouble to explain it

没有评论:

发表评论