Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Abt bitwise complement operator!

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class test {
public static void main(String args[]) {
byte x = 3;
x = (byte)~x;
System.out.println(x);
}
}
The above ex. prints a result -4 , i dont understand how it prints -4 without printing 256.the complement of 3 in byte rep is 0000 0011 and its complement is 1111 1100 and if this is been converted to decimal then will it not give a result 256, how come -4?
Can anyone pls explain this?
 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings Pradeepa,
The key to this is the line:
x = (byte)~x;
In this line the (byte) cast is necessary as ~x is an int value. You take 3 and complement the result is -4 (int). now cast from -4 (int) to (byte) and you will get -4 (byte) is an 8 bit signed number with a range of -128 to 127.
Steve

[This message has been edited by Steve Fahlbusch (edited January 19, 2001).]
 
Pradeepa Saiganesh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks steve
 
I'm full of tinier men! And a tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic