aspose file tools*
The moose likes Beginning Java and the fly likes Negative int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Negative int" Watch "Negative int" New topic
Author

Negative int

visu Nekk
Ranch Hand

Joined: Sep 06, 2005
Posts: 122
Hi,

Given an int, how to determine whether it is positive or negative. For e.g. int x = -1; How to determine x is negative?

Thanks,
Visu Nekk
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

look at the bit pattern of x..!!

if right most bit is 1, this means it is negative..!!!

x = -1 ===> 11111111 11111111 11111111 11111111


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I'm not sure the original poster was thinking of bit patterns. The answer might be as simple as


"I'm not back." - Bill Harding, Twister
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

if he was asking this..!! he could have tested it first..before posting!!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
This is the beginner section. Maybe he doesn't know how to use <, or write an if statement, or print the result. Who knows?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18910
    
    8

And also, if the rightmost bit of a binary integer is 1, that means it's an odd integer, not that it's a negative integer.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
Originally posted by Paul Clapham:
And also, if the rightmost bit of a binary integer is 1, that means it's an odd integer, not that it's a negative integer.
You mean leftmost, surely.

BTW: If you get a binary String with the Integer.toBinaryString() methods, it excludes leading 0s, so it always has 1 as the leftmost displayed digit. You will have to use a for loop and the insert method of StringBuilder (or similar) to pack it with 0s from the left.

[edit]For always has 1 read "almost always has 1" The value of 0 wouldn't have a 1 in![/edit]
[ January 30, 2008: Message edited by: Campbell Ritchie ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[PC]: And also, if the rightmost bit of a binary integer is 1, that means it's an odd integer, not that it's a negative integer.

[CR]: You mean leftmost, surely.


I'm pretty sure he meant rightmost. Leftmost is what Sunny should have said, but rightmost is what he did say. Paul was commenting on that.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Ohh I am sorry, What I meant was left most bit..!!
Being a left handed person i often got confusion regarding that..! as My left is actually my right hand..but for other it is left hand..!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
Yes, you are right; it was Sunny Jain who ought to have said leftmost.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Originally posted by Sunny Jain:
Ohh I am sorry, What I meant was left most bit..!!
Being a left handed person i often got confusion regarding that..! as My left is actually my right hand..but for other it is left hand..!!


Good that the confusions are resolved earlier here. Also i believe it does not make any confusions wiht L-R , R-L associativities


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Negative int