Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization 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
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Question on Bitwise Complement Operator

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the copy/paste from K&B Book for SCJP 1.4

The ~ operator is a flip-the-bits operator. It will change all 1s to 0s and vice versa.
Look at the following code:

This program is changing every bit into its complement; thus, the output from this program is the following:
%java Bitwise
x is initially 5
~x is equal to -6
In bit representation, the conversion looks like this,
~0000 0000 0000 0000 0000 0000 0000 0101
and converts to
1111 1111 1111 1111 1111 1111 1111 1010



My Question is how this value 1111 1111 1111 1111 1111 1111 1111 1010 is equal to -6 Some please explain me..
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1111 1111 1111 1111 1111 1111 1111 1010 = -6 is one of binary negative presentation.

u can think about this way:

1000 0000 0000 0000 0000 0000 0000 0000 (=-2147483648)
+ 0111 1111 1111 1111 1111 1111 1111 1010 (=2147483642)
-------------------------------------------------------------
1111 1111 1111 1111 1111 1111 1111 1010 = -6
 
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic