jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes simple bitwise complement ~ question, help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "simple bitwise complement ~ question, help" Watch "simple bitwise complement ~ question, help" New topic

simple bitwise complement ~ question, help

sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
can someone explain the outcome of the following
using bytes or some other formula
is this formula valid in these circumstances?
1. change sign and subtract one ?
Anubala shrivastava

Joined: Nov 03, 2000
Posts: 9
~ is a bitwise inversion operator .It just invert bytes from 0 to 1.
4 can be written as 0000 0000 0000 00100 in bits presentation
and ~4 will be------1111 1111 1111 11011 this is just inversion of bytes.since higher order bit is 1 it means this must be negative number,ans accordin to java negative no represent in two's complement.In order to see magnitude of two's complements no just invert bit and add 1 bit in lower order bit.
So it wiil be 0000 0000 0000 00100
+ 1
= 0000 0000 0000 00101
When you convert it into decimal no it will be 5
so ~4 will be -5.
If I m wrong please anybody explain it.
Rob Levo
Ranch Hand

Joined: Oct 01, 2000
Posts: 167
Amit Tyagi
Ranch Hand

Joined: Oct 18, 2000
Posts: 52
hI sarim ,
above things are exactly correct.
But u can follow this thumb rule also as you won't be getting time to calculate all this.
~x = -(x)-1
here x is a signed number means ..if
x =4 then -(4)-1 = -5
x =-4 then -(-4)-1 = 3
x = 0 then -(0)-1 = -1
hope this will help you.....
sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
thanks everyone, and amit you seem to be a java stud!
It is sorta covered in the JavaRanch Style Guide.
subject: simple bitwise complement ~ question, help
It's not a secret anymore!