查看完整版本 : java negative number comparsion

linuxfedora 2017-11-22 09:12 AM

java negative number comparsion

why it will return false? How can i check the bit 31 is 1 correctly for negative number on java?? Thanks[code]        
long value = -2147483648l; //1000000000000000

if((value & (long)(1<<31)) > 0)
{
System.out.println("true");
}
else
{
System.out.println("false");
}[/code]

[[i] 本帖最後由 linuxfedora 於 2017-11-22 09:39 AM 編輯 [/i]]

assembly.jc 2017-11-22 11:36 AM

long 是 64-bit 的

linuxfedora 2017-11-22 11:38 AM

long 是 64-bit 的 [/quote]

Yes, i knew, but the bit on 32 for long -1 should be 1?? And i right? Thanks
is -1L in binary is 11111111 111111111 11111111 11111111 [b]1[/b]000000 00000000 00000000 00000000?

[[i] 本帖最後由 linuxfedora 於 2017-11-22 11:41 AM 編輯 [/i]]

assembly.jc 2017-11-22 12:09 PM

[quote]原帖由 [i]linuxfedora[/i] 於 2017-11-22 11:38 AM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=471280853&ptid=27074998][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
long 是 64-bit 的

Yes, i knew, but the bit on 32 for long -1 should be 1?? And i right? Thanks
is -1L in binary is 11111111 111111111 11111111 11111111 1000000 00000000 00000000 00000000? [/quote]

1 is int not long, even though you cast it to long after shift operation.[code](long) (1<<31)[/code]change 1 to 1L and try again[code](1L << 31)[/code]

[[i] 本帖最後由 assembly.jc 於 2017-11-22 12:11 PM 編輯 [/i]]

linuxfedora 2017-11-22 12:26 PM

[quote]原帖由 [i]assembly.jc[/i] 於 2017-11-22 12:09 PM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=471282482&ptid=27074998][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]


1 is int not long, even though you cast it to long after shift operation.(long) (1 [/quote]


thanks, i understood.
頁: [1]
查看完整版本: java negative number comparsion