Negative number

S

snehapshinde

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.
 
J

Joshua Cranmer

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.
 
S

snehapshinde

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.
 
S

snehapshinde

Patricia: it's the same poster, actually ;)



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.
 
M

Mark Space

Joshua said:
Java treats all integral types as signed.

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

Mike Schilling

Mark said:
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.
 
B

blue indigo

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.
 
R

Roedy Green

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top