2013年9月17日星期二

About JAVA give int and short assignments in hexadecimal form of the value of the

 This post last edited by the sd3560531 on 2011-06-28 16:27:47
know int occupies 32 , the highest bit is the sign bit so I gave him an assignment as a hex
int a = 0xFFFFFFFF; compiler without error. Represents -1 ;

The byte representing 8 , the highest bit is the sign bit ,
reasonable to say : byte = 0xFF; also should be correct . ( 11,111,111 ) represents -1 ;
But this assignment , the compiler error.
where:
byte s = 0x7F; is correct. Represent 127.
byte s = 0x80; wrong. This should be ( 10000000 represents -128 )

looks like a bit of a let up the assignment , the compiler that is wrong.
I ask is that those above byte assignment , why is it wrong ? Why not it is consistent with the int ?

with similar byte , short is such a problem , short s = 0x7fff; right and short s = 0x8000; Error

Yes, my JDK version 1.6.0_10



------ Solution ------------------------------------ --------
A hexadecimal numeral consists of the leading ASCII characters 0x or 0X followed by one or more ASCII hexadecimal digits ; and can represent a positive, zero, or negative integer . Hexadecimal digits with values ​​10 through 15 are represented by the ASCII letters a through f or A through F, respectively; each letter used as a hexadecimal digit may be uppercase or lowercase.
0x beginning of the figures are of type int instead of byte or short types.
------ Solution ---------------------------------------- ----


Yes , int is 32 landlord so copying, no more than natural viable strategy.
------ For reference only -------------------------------------- -

byte b= -1;//没有任何问题,因为byte可以表示-128到127之间的数
System.out.println(0xFF);//255,这里的0xff实际上等价于0x000000ff,已经超过了127,当成了int
byte s = -128;//没有任何问题,因为byte可以表示-128到127之间的数
System.out.println(0x80);//128,同样的道理,等价于:0x00000080,已经超过了127,当成了int
byte s2 = 0x7f;
System.out.println(0x7f);//127,同样的道理,等价于:0x0000007f,没有超过127

------ For reference only ------------------------------- --------
My question is
byte s2 = 0x80; Why is it wrong ? Supposed to represent -128 .
If 0x80 exceeds 127 , it should be automatically turned negative ah.

like int will be automatically turned negative .
int i = 0x7fffffff; expressed int max
The assignment int i = 0x80000000; compiler does not think over , and to automatically switch to a negative number . Int represents the minimum .

------ For reference only ---------------------------------- -----
learned
------ For reference only ---------------------------- -----------
you mean :
like this assignment byte b = 0x80; exist internal type conversion , 0x80 represents 128 , internal conversion , when more than 127 so error.
The int i = 0x80000000; does not exist internal conversion , directly to 0x8000000 should represent the value of the assignment to i a .

Is that right ?
------ For reference only -------------------------------------- -
grateful to answer ah period where English is the excerpt ?

没有评论:

发表评论