Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Bitwise Operators. Khalid Mughal.

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bitwise Operators. Khalid Mughal.

class bitwiseOperators
{
public static void main(String[] args)
{
char v1 = ')';// Unicode value 41
int result1 = ~v1;
System.out.println(" ~v1 = " + result1);
}
}

Output is :

~ v1 = -42

Can anyone explain me above output in detail.
Code is from Khalid Mughal (jdk 1.4) page 76
In detail I mean, what is value of v1 in binary, then what is its complement binary value, then what is its decimal value, etc. I know how to convert decimal to binary and vice versa.
When I am doing this program on PC I am getting correct answer. But when I am trying on piece of paper I got 22 as answer.

Pankaj Shinde
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, note that bit manipulations are included on the 1.4 exam, but not on the 1.5 exam.

We know this represents a negative value, because the lead bit (the "sign bit") is 1. So to determine the value, we invert the bits, then add 1. Inverting the bits just brings us back to what we started with...

0000 0000 0000 0000 0000 0000 0010 1001

Then adding 1 gives us...

0000 0000 0000 0000 0000 0000 0010 1010

...which is 42. So the value is -42.
[ April 12, 2007: Message edited by: marc weber ]
 
Pankaja Shinde
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you sir
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic