The java bit arithmetic shift operation and

Bit computing

java has four bit operations, namely, "& bitwise and, bitwise or |, ^ bitwise exclusive OR, bitwise ~", their rules of operation are:

  1. Bitwise AND &: Two all 1, the result is 1
  2. Bitwise or |: there is a two to one, the result is 1
  3. Bitwise XOR ^: 0 of a two, a is 1, the result is a
  4. Bitwise ~ :( For signed numbers, including sign bit) = 0> = 1, 1> 0

For example: 2 & 3 = 2 2 | 3 = 3 2 ^ 3 = 1 ~ 2 = -3

Shift operation

java has three shift operator:
>>, and << Arithmetic shift right arithmetic shift left, arithmetic rules:
arithmetic shift right: low overflow, sign bits unchanged, with the sign bit and overflow high
arithmetic shift left: Symbol then keeping low fill 0

>>> Logical shift right arithmetic rules are: low overflow, high fill 0

比如,-1>>2=-1,1<<2=4,3>>>1=1

note

Computer operations are complement arithmetic

Published 75 original articles · won praise 0 · Views 1499

Guess you like

Origin blog.csdn.net/qq_34087914/article/details/104168769