It's not a secret anymore!
The moose likes Beginning Java and the fly likes Some arithmetic problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Some arithmetic problems" Watch "Some arithmetic problems" New topic

Some arithmetic problems

Antriksh Verma
Ranch Hand

Joined: Nov 24, 2008
Posts: 57
I faced this problem

int x = -8;
int y = ~-33;

1. what is ~ operator ?
2. Please explain >>> operator & its effect here on both x & y ?
3. what is value of y , after ~ .
Keith Rainey
Ranch Hand

Joined: Jan 19, 2011
Posts: 66

Taken from Oracle's Java Tutorial Pages

"The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension."

"The unary bitwise complement operator "~" inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a byte contains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111"."

Keith Rainey
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

You can learn about all the operators in Java from This Tutorial. The ~ is a 'bitwise compliment' operator, which inverts the bit-value for each binary bit in a number. The >>> is a a bit shift operator that moves a 0 in to the 'sign bit' (the left bit position) essentially forcing it to be positive.

So what are the effects of these operators? The best way to see that is to convert your numbers to their binary representation then perform the operations by inverting and shifting as appropriate.

I agree. Here's the link:
subject: Some arithmetic problems
It's not a secret anymore!