Win a copy of Design for the Mind this week in the Design forum!

# Some arithmetic problems

Antriksh Verma
Ranch Hand
Posts: 61
I faced this problem

int x = -8;
int y = ~-33;
x>>>y;
SOP(x);

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
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"."

Steve Luke
Bartender
Posts: 4181
21
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.