Skip to content

Instantly share code, notes, and snippets.

@jamesyang124
Last active June 6, 2018 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesyang124/621439ab5065405f51fb8365d26fe920 to your computer and use it in GitHub Desktop.
Save jamesyang124/621439ab5065405f51fb8365d26fe920 to your computer and use it in GitHub Desktop.
java-operations

Logical Operator

  • ^ as XOR operator
  • & is AND operator
  • | is OR operator
  • && and || are short-circuit operators.

Bitwise Operation

  • 2's complement represent negative number x as its (abs(x) - 1) binary format, then flip each 0 and 1 bit.

1's complement

The ones' complement of a binary number is defined as the value obtained by inverting all the bits in the binary representation of the number (swapping 0s for 1s and vice versa). The ones' complement of the number then behaves like the negative of the original number in some arithmetic operations.

Bits unsigned value 1's
0000 0001 1  1 
0000 0010 2  2 
0000 0000 0  0 
0111 1110 126 126 
0111 1111 127 127 
1000 0000 128 −127 
1000 0001 129 −126 
1111 1101 253 −2 
1111 1110 254 −1 
1111 1111 255 −0

https://en.wikipedia.org/wiki/Ones%27_complement

  • 2's complement can be represent by 1's then add 1

invert(x) + 1 ex: (111)

  • ^ is XOR operator for each bits. ex: 100 ^ 101 == 001
  • | is OR operator
  • & is AND operator
  • ~ is bitwise complement operator, ex: ~1000 ==

shift operators

  • >> signed right shift, shifted bits will moved, and highest signed bit will preserve.

1001 >> 1 === 1100

  • o.w. >>> shift ops will prepend back 0 in all cases, ex: >>>, <<.
  • >> represent as divide by 2^n , << as multiply by 2^n

type casting

for data type's size is shorter than int ex: byte, char, or short, bitwise opertor will be type conversion to int, which may leads unexpected error:

byte i = -2
i >>> 1
// expect 11111110 => 01111111 => 127
// but since i upcast to int, actual value is 2147483647

so for unary operator byte, char, or short will be cast to int. but for binary operator, rules will be applied in order:

  1. if any operand is double, both cast to double
  2. if any operand is float, bot cast to float
  3. if any operand is long, bot cast to long
  4. o.w. cast to int

To ensure we only want to use operator as byte, short, char, we can type casrting it:

byte i = -2;
i = (byte) (i >> 1);
// i == -1

But it might raise overflow issue, ex:

int i;
byte b;
i = 199;

b = (byte) i;
// -57
// bits over value 127 are truncated.
  • literals ex: 1, -1, 298 will be type casting to int by default, floating number ex: 1.23 will be double

Precedence and associativity

list by its level:

  • ++, --, unary +, - as positive, negative sign, ~, !, (type casting) => R
  • *, /, % => L
  • +, -, string concat + => L
  • >>, >>>, << => L
  • <, <=, >, >=, instanceof => L
  • ==, != => L
  • & => L
  • ^ => L
  • | => L
  • && => L
  • || => L
  • ?: ternary operator => R
  • =, *=, /=, %=, +=, -=, <<=, >>=, >>>=, &=, |=, ^= assignment operators => R

ternary operators

  • if condition not hold, the corresponding expression will not be excuted:
int i, j = 17;
i = j % 2 == 1 ? 2 : j++
// j++ will not meet, not executed though have high precedence.
  • type castign rule for 2nd and 3rd expressions:
  1. for ? x : y, if x and y have same return type, keep return result as same type.
  2. for ? x : y, if x is byte and y is short type, keep return result as most extended type, so to be short.
  3. for ? x : y, if x is byte and y is an expression by integer literals (1) as int, if result is fall into the range of byte, will return as byte.
  4. o.w. follow above binary operators type casting rules.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment