• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Negative int

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look at the bit pattern of x..!!

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

x = -1 ===> 11111111 11111111 11111111 11111111
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure the original poster was thinking of bit patterns. The answer might be as simple as
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if he was asking this..!! he could have tested it first..before posting!!
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the beginner section. Maybe he doesn't know how to use <, or write an if statement, or print the result. Who knows?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are right; it was Sunny Jain who ought to have said leftmost.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 8
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



OUTPUT:

Number '-7' Binary Representation as 11111111111111111111111111111001
-ve Number
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is the problem with toBinaryString(); it doesn't include leading 0s. That method has had that feature for so long that it would be impossible to change it now. So you have to test for its length. In fact, I think that testing for length() == 32 will be sufficient without testing for starting with 1. All Strings returned from that method start 1 
There are various other things you can try, all of which will tell you whether the lowest bit is set:-Details of method here. Sorry if line 3 is too complicated.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A few minutes ago, I wrote: . . . All Strings returned from that method start 1  . . . .

. . . and made the same mistake as ten years ago, when I was a humble Ranch Hand (well, maybe I wasn't humble). The binary representation of 0 is "0".
 
He does not suffer fools gladly. But this tiny ad does:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic