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
• Ron McLeod
• Rob Spoor
• Tim Cooke
• Junilu Lacar
Sheriffs:
• Henry Wong
• Liutauras Vilda
• Jeanne Boyarsky
Saloon Keepers:
• Jesse Silverman
• Tim Holloway
• Stephan van Hulst
• Tim Moores
• Carey Brown
Bartenders:
• Al Hobbs
• Mikalai Zaikin
• Piet Souris

# Not Operator

Greenhorn
Posts: 25
• • Number of slices to send:
Optional 'thank-you' note:
• • What will be the output when you compile and execute the following program.

public class Base{
private void test() {
System.out.println(~6);
}

static public void main(String[] a) {
new Base().test();
}
}

a) 0
b) false
c) -6
d) Compilation Error.Incompatible type for ~. Can't convert int to boolean.
e) ~6
f) -7
Correct answer is -7 . I thought it will be 1.
since
~110=001
How it become -7???

Ranch Hand
Posts: 82
• • Number of slices to send:
Optional 'thank-you' note:
• • I'm going to use the byte representation for my explanation though the actual operation happens using ints.
6 == 0000 0110
~6 == 1111 1001
which is the 2's complement representation of -7.
here's a nice way to convert binary to decimal for negative numbers:
when u see a negative bit pattern (i.e. the most significant bit is 1), invert all bits, convert to decimal and add 1. then stick a negative sign to it.
so, in the case of 1111 1001,
invert all bits:
0000 0110
convert to decimal:
0000 0110 = 6
=7
stick that minus:

Greenhorn
Posts: 15
• • Number of slices to send:
Optional 'thank-you' note:
• • This is just a tip:

When the not operator is applied to number:
i)if the number is positive,increase the number by one and make it negative.This is the result when the not operator is applied to positive number as in:
int i=5678;
int j=~i;
j is -5679
ii)if the number is negative decrease the number by one and make it positive.
int i=-67889;
int j=~i;
j is 67888

[This message has been edited by Saritha Pilla (edited March 04, 2000).]

Divakar
Greenhorn
Posts: 25
• • Number of slices to send:
Optional 'thank-you' note:
• • Thanx Rolf,Sritha 