Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Reg. Bitwise NOT  RSS feed

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the BITWISE NOT ~ give the two's complement
of a number or one's complement ?
 
Gold Digger
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bitwise NOT (~) inverts all bytes of a number, i.e. it returns it's 1's complement,you got the 2's complement by negating the number.
18 => 00000000000000000000000000010010
~18 => 11111111111111111111111111101101 = -19
-18 => 11111111111111111111111111101110 = ~18+1
to sum up the 2's complement is equals to the 1's complement + 1
Anyone correct me if I messed up with those bits

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angela
Further to Val's example A bit of code to as proof of concept.The results of ones/twos complement rules always seemed counter-intuitive to me!
Hi Angela
Bitwise Complement (~) works as a one's complement As an example:
class BitTest{ //bytes for berevity!
public static void main(String[] args){
byte i = 23; //0001 0111
byte result;
result = (byte)~i; // ~ 0001 0111 --> 1110 1000
System.out.println(result); // prints -24
System.out.println(i); // prints 23
}
}
Hope this helps - Jim
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Val and Jim. I got it now.
All i can conclude is that to compute the BITWISE NOT
of value follow the formula :
~i = (-i)-1
Correct me if wrong.
 
Jim Petersen
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I think so
I think in words better than numbers so I think of the ~ "giving you one less than you think"
nb ~ works the same the other other way: ie for negatives as well so ~(-23) returns 22
All the best - Jim
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!