Velocity Reviews > Java > Negative number

# Negative number

snehapshinde@gmail.com
Guest
Posts: n/a

 01-21-2009
Hello,
Following is a java snippet,

System.out.println(Integer.toBinaryString(5));
System.out.println(Integer.toBinaryString(~5));
System.out.println(~5);

The output is:
101
11111111111111111111111111111010
-6

First is the binary of 5.
Second is binary inversion of 5.
Third line displays the decimal form of ~5
How to know that inversion of 5 is decimal -6 and not decimal
4294967290(I have calculated this value using calc).
Plz help.Thanks.

Joshua Cranmer
Guest
Posts: n/a

 01-21-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> How to know that inversion of 5 is decimal -6 and not decimal
> 4294967290(I have calculated this value using calc).

Java treats all integral types as signed.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

snehapshinde@gmail.com
Guest
Posts: n/a

 01-21-2009
On Jan 21, 4:53*pm, Patricia Shanahan <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > Hello,
> > Following is a java snippet,

>
> > System.out.println(Integer.toBinaryString(5));
> > System.out.println(Integer.toBinaryString(~5));
> > System.out.println(~5);

>
> > The output is:
> > 101
> > 11111111111111111111111111111010
> > -6

>
> > First is the binary of 5.
> > Second is binary inversion of 5.
> > Third line displays the decimal form of ~5
> > How to know that inversion of 5 is decimal -6 and not decimal
> > 4294967290(I have calculated this value using calc).
> > Plz help.Thanks.

>
> Did you read thehttp://en.wikipedia.org/wiki/Two%27s_complementlink
> posted in response to a similar question on another thread?
>
> See the second sentence of the "Explanation", "The boundary between
> positive and negative numbers is arbitrary, but the de facto rule is
> that all negative numbers have a left-most bit (most significant bit) of
> one."
>
> Java signed integer types are required to behave as though they are 2's
> complement with the boundary set so that a number is negative if, and
> only if, its most significant bit is one.
>
> ~5 is negative because it is an int, the 32 bit signed integer type, and
> ~5 has a 1 in the most significant of those 32 bits.
>
> Patricia- Hide quoted text -
>
> - Show quoted text -

OK, it means that while deciding that the number is positive or not we
should consider the number of bits used for that data type.
Thanks,Patricia.

snehapshinde@gmail.com
Guest
Posts: n/a

 01-21-2009
On Jan 21, 5:56*pm, Sabine Dinis Blochberger <(E-Mail Removed)>
wrote:
> (E-Mail Removed) wrote:
> > On Jan 21, 4:53 pm, Patricia Shanahan <(E-Mail Removed)> wrote:

>
> > > Did you read thehttp://en.wikipedia.org/wiki/Two%27scomplementlink
> > > posted in response to a similar question on another thread?

>
> Patricia: it's the same poster, actually
>
> > > ~5 is negative because it is an int, the 32 bit signed integer type, and
> > > ~5 has a 1 in the most significant of those 32 bits.

>
> > OK, it means that while deciding that the number is positive or not we
> > should consider the number of bits used for that data type.

>
> An integer number occupies the same number of bits, whether signed or
> not.
>
> It seems to me you lack understanding of computer science basics. Go to
> the wikipedia link, and also look at the other articles it links to.
>
> <http://en.wikipedia.org/wiki/Two's_complement>
>
> Here's a good place to start to get all bases covered
> <http://en.wikipedia.org/wiki/List_of_basic_computer_science_topics>
>
> Are you studying a computer science course at the moment? You should let
> your instructor know about your difficulties of comprehension.

I have just started with basics.
anyway, thanks for the suggession.

Mark Space
Guest
Posts: n/a

 01-21-2009
Joshua Cranmer wrote:
> (E-Mail Removed) wrote:
>> How to know that inversion of 5 is decimal -6 and not decimal
>> 4294967290(I have calculated this value using calc).

>
> Java treats all integral types as signed.
>

Except for "char" ... because consistency would be bad, and mixing
signed and unsigned types never causes confusion.

Mike Schilling
Guest
Posts: n/a

 01-21-2009
Mark Space wrote:
> Joshua Cranmer wrote:
>> (E-Mail Removed) wrote:
>>> How to know that inversion of 5 is decimal -6 and not decimal
>>> 4294967290(I have calculated this value using calc).

>>
>> Java treats all integral types as signed.
>>

>
> Except for "char" ... because consistency would be bad, and mixing
> signed and unsigned types never causes confusion.

And including byte, even though bytes are (like chars) almost never
used in arithmetic and (as with chars) sign-extending them is almost
never desirable.

blue indigo
Guest
Posts: n/a

 01-21-2009
On Wed, 5622 Sep 1993 02:28:36 -0800, snehapshinde wrote:

> Hello,
> Following is a java snippet,
>
> System.out.println(Integer.toBinaryString(5));
> System.out.println(Integer.toBinaryString(~5));
> System.out.println(~5);
>
> The output is:
> 101
> 11111111111111111111111111111010
> -6
>
> First is the binary of 5.
> Second is binary inversion of 5.
> Third line displays the decimal form of ~5
> How to know that inversion of 5 is decimal -6 and not decimal
> 4294967290(I have calculated this value using calc).
> Plz help.Thanks.

As others have noted, Java's "int" type is signed and 2s-complement.
As a result, every integer n will be changed by ~ to -n-1 (that is,
negated and one subtracted).

Put the bit pattern for ~5 into the low 32 bits of a Java "long", though,
with zeros for the high 32 bits, and you would indeed get 4294967290. Same
if you bit-inverted a C "unsigned int" on a platform with ints 32 bits
wide (printf("%d",(unsigned int)~5) or an "unsigned long" on a platform
with longs 32 bits wide.

Most of the time, Java programmers don't need to worry overly much about
the exact bit representation of integers. Even when using them in I/O,
since the default representation for all Java implementations is the same
regardless of host architecture.

The major exceptions are:
* Using large values. You may need long or even BigInteger, depending.
* Doing binary-format I/O with non-Java tools reading files, writing
files, or networking with your Java one, including reading common
binary file formats like .gif. Mostly there are existing library
routines (ex. JAI/ImageIO classes in the case of .gif) for common
file formats.
* Doing work with bytes. Java's "byte" type is, unfortunately, signed.
* Doing arithmetic with chars. Java's "char" type is UNsigned.
* Implementing compare() and compareTo(). Naive subtraction could result
in an unwanted wraparound eventually. Best is to return -1, 0, or 1 as
appropriate.
Poor: return this.intField - that.intField;
Good: if (this.intField < that.intField) return -1;
if (this.intField > that.intField) return 1;
return 0;
The "poor" version has problems if the intField values get widely enough
separated. The difference might be bigger than 2147483647 in magnitude,
and then the sign of the returned value would be exactly opposite what
is desired! The "good" version, meanwhile, makes it easy to add more
subordinate sort criteria by inserting other stuff before "return 0".
Ex. if (this.intField < that.intField) return -1;
if (this.intField > that.intField) return 1;
if (this.intField2 < that.intField2) return -1;
if (this.intField2 > that.intField2) return 1;
return 0;
sorts on intField and, for objects with equal intFields, sorts on
intField2. Real world example could be sorting on last name, but
for equal last names earlier first names come first instead of all
comparing equal for position and therefore being jumbled up.

--
blue indigo
UA Telecom since 1987

Roedy Green
Guest
Posts: n/a

 01-27-2009
On Wed, 21 Jan 2009 02:28:36 -0800 (PST), (E-Mail Removed)
wrote, quoted or indirectly quoted someone who said :

>How to know that inversion of 5 is decimal -6 and not decimal
>4294967290

any string of bits can be interpreted as BOTH a signed or unsigned
quantity.

see http://mindprod.com/jgloss/binary.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Here is a point of no return after which warming becomes unstoppable
and we are probably going to sail right through it.
It is the point at which anthropogenic (human-caused) warming triggers
huge releases of carbon dioxide from warming oceans, or similar releases
of both carbon dioxide and methane from melting permafrost, or both.
Most climate scientists think that point lies not far beyond 2°C (4°F) C hotter."
~ Gwynne Dyer

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post MRAB Python 2 01-31-2009 02:41 AM EK Python 0 01-30-2009 04:13 AM prem_eda VHDL 5 10-11-2004 12:14 PM JKop C++ 16 06-05-2004 05:42 PM Jason C++ 25 02-22-2004 08:43 PM

Advertisments