• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

casting

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Casting
{ public static void main(String []s)
{
long l=130L;
byte b=(byte)l;
System.out.println("the byte is:"+b);
}
}
compiles fine and output is :the byte is -126

how is that possible i converted 130 to binary it came out to be '10000010' then how the answer is -126 please explain step by step.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how is that possible i converted 130 to binary it came out to be '10000010' then how the answer is -126 please explain step by step.



Believe it or not, for a byte, 10000010 binary is -126 decimal. Java uses "twos complement" to represent numbers.

http://en.wikipedia.org/wiki/Two's_complement

Henry
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


compiles fine and output is :the byte is -126



The first bit of any of the signed primitives represents the sign. You can calculate the negative by inverting all the bits adding 1 and making it negative.

1000 0010 --> 0111 1101 == 2^0 + 2^2 + 2^3 +2^4 +2^5 +2^6 == 125 ...now add 1 and make it negative == -126

If this was a short(16 bits) instead of a byte it would be able to represent more bits. You could cast your 130 into the short and it would represent 130 just fine because the first bit wouldn't be on. However once you try to represent a number that would turn the first bit on (around 32000 ish) it turns the number negative.
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Henry and paul i have one doubt both of you told that it will give -126. it definitely give 126 but,how it will get -(negative).the most significant bit is 0 after doing 2's complement. if it is 1 we will get negative, but it is 0 so we need to get positive. but why we got negative???
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think nagaraju you are not understanding the concept. You must have given your focus to this statement by Paul

You can calculate the negative by inverting all the bits adding 1 and making it negative.



This is what he just did.
 
Paul Yule
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because these types also represent negative number the first bit represents the sign of the number so

1xxx xxxx is negative for a byte
1xxx xxxx xxxx xxxx is negative for a short
our 130 is 1000 0010 as a byte (-126)
and 130 is 0000 0000 1000 0010 as a short (130) even though short represents negative numbers as well the first bit is not on.

It is the first bit that matters prior to doing any calculations. If this was an unsigned data type then it's values would all be positive and there would be no need to do any method of calculation for bits. As is the case of a char.

so if byte represented positive numbers only [0-255] instead of[-128, 127]

our 130 as 1000 0010 would be (130). No inverting bits or anything...just calculating the number.

However, since bytes do represent both positive and negative numbers the first bit effectively allots half to negative and half to positive numbers with zero being considered positive.

Originally posted by nagaraju uppala:
the most significant bit is 0 after doing 2's complement.


You only need to invert bits if it's negative...and if you inverted bits because it's negative (which would effectively always turn the first bit to zero) don't confuse yourself with "hmm, it's a zero now in the first bit must not be negative".

I think of it like this...you go from the range of positive numbers to the the range of negative numbers. You start with the smallest positive number and go to the highest positive number 0000 0000 to 0111 1111 [0,127] then you go to the smallest negative number and work your way to the largest negative number 1000 0000 to 1111, 1111 [-128, -1]
[ December 12, 2008: Message edited by: Paul Yule ]
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic