Help coderanch get a
new server
by contributing to the fundraiser
  • 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

int value cast to byte

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
class E {
static byte a = (byte)127;
static byte b = (byte)128;
static byte c = (byte)255;
static byte d = (byte)256;
public static void main(String args[]) {
System.out.print(a + " " + b + " " + c + " " + d);
}
}
the answer is 127 -128 -1 0.
can anyone explain me what exactly happens when int value is converted to byte value?
thanks,
basanti
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Basanti Mathad:
Hi,
class E {
static byte a = (byte)127;
static byte b = (byte)128;
static byte c = (byte)255;
static byte d = (byte)256;
public static void main(String args[]) {
System.out.print(a + " " + b + " " + c + " " + d);
}
}


When int variable is casted to a byte var, only the lowest 8 bits are used. in your example,
127 is 0000,0000,0000,0000,0000,0000,0111,1111
cut the lowest 8 bits, the result is 0111,1111
128 is 0000,0000,0000,0000,0000,0000,1000,0000
do the same thing, the result is 1000,0000 (-128)
255 is 0000,0000,0000,0000,0000,0000,1111,1111
the result is 1111,1111 (-1)
256 is 0000,0000,0000,0000,0000,0001,0000,0000
the result is 0000,0000 (0)
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integral values in Java are stored in a format called signed two's complement. Negative numbers are expressed using the high-order bit to express sign: if the high bit is zero, the 7-bit value that follows is positive. if the high bit is one, the value is negative.
(Forget what I said here before)
Complementary values follow their own logic here: Because 01111111 and 1000000 are complements, they represent maximum and minimum values. Overflow describes the behavior of subtracting from the logical minimum or adding to the maximum. So 10000000 - 00000001 gives you 01111111 or the logical maximum (and that's the one that always twists my brain and makes me say wrong things ).
Conversely, adding one to the logical maximum, or 01111111 + 00000001 gives you 10000000 or the logical minimum, and also makes me say wrong things.
This has two benefits, if memory serves: one, overflowing integrals doesn't throw exceptions, which we like. We put it on the programmer's shoulders to know the ranges of their integrals and be wary of boundary conditions. Two, every other increment or decrement works as expected.
[ November 19, 2002: Message edited by: Michael Ernest ]
 
Basanti Mathad
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
I got it
 
Basanti Mathad
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
I got it
 
Basanti Mathad
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class J {
public static void main (String[] s) {
byte b = 0;
b += ~b >>> 1;
System.out.println(b);
}
}

what would be the answer for this and why?
thanks,
basanti
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
byte b=0;
b+=~b>>>1;
first this exp is:
b = b+ ~b>>>1;
sub the values;
b=0 + (-1)>>>1;
=-1>>>1
b=-1
NOTE: ~x=-(x+1)
hope this helps
thx
 
Basanti Mathad
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey anushree,
how is -1>>>1 = -1 ?
can u explain?
thank u,
basanti
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte b = 0;
b += ~b >>> 1;
is same as doing
b = (byte) (b + (~((int) b) >>> 1));
Answer is -1.
1. b become 16 zeros. 0000 0000 0000 ...
2. Invert. becomes 16 ones. 1111 1111 ...
3. Add 2 to 16 zeros, you get 16 ones.
4. Chop off high order bits you have 8 ones which is -1.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class J2 {
public static void main (String[] s) {
byte b = 0;
//b += ~b >>> 1; <==> b = (byte) ( b + (~b >>> 1) );
//first we change 0s per 1s
System.out.println(~b);//11111111 11111111 11111111 11111111 //-1
//second, >>> introduces a 0 for the left and all the remaining 1s are right shifted, the rightmost
// is lost. >>> always introduce 0. >> introduces the same digit as there was in the leftmost
// position before the shift (keeping the sign)
System.out.println(~b >>> 1);//01111111 11111111 11111111 11111111 //2147483647
System.out.println(b + ((~b >>> 1)));//01111111 11111111 11111111 11111111 //2147483647
//the += operator implies a casting to the type of the variable b that is, byte. All except
//the 8 least significant bits are truncated:
System.out.println((byte) (b + ((~b >>> 1))));//11111111 //-1
}
}
 
This tiny ad is guaranteed to be gluten free.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic