Java: Что означает ~

Путем объявления их как краткого обзора Вы в действительности указываете другим кодерам, что намеревались для этих классов быть полученными из. Действительно, Вы правы, что нет большого различия, но семантика здесь действительно больше об интерпретации других людей, которые смотрят на Ваш код.

72
задан Eric Leschinski 22 June 2014 в 00:26
поделиться

4 ответа

The Tilde (~) performs a bitwise complement of a numerical value in Java.

See: Bitwise complement (~): inverts ones and zeroes in a number

66
ответ дан 24 November 2019 в 12:39
поделиться

It is the Unary ~ Bitwise complement operator (quoting) :

  • only used with integer values
  • inverts the bits ie a 0-bit becomes 1-bit and vice versa
  • in all cases ~x equals (-x)-1

See also this page on Bitwise operators on wikipedia, which states :

The bitwise NOT, or complement, is a unary operation that performs logical negation on each bit, forming the ones' complement of the given binary value. Digits which were 0 become 1, and vice versa.
For example:

NOT 0111  (decimal 7)
  = 1000  (decimal 8)

In many programming languages (including those in the C family), the bitwise NOT operator is "~" (tilde).

52
ответ дан 24 November 2019 в 12:39
поделиться

Как было сказано ранее ~ - унарный побитовый оператор НЕ.
Your example tests whether modifiers contains bits other than those defined in KeyEvent.SHIFT_MASK.

  • ~KeyEvent.SHIFT_MASK -> all bits except those in KeyEvent.SHIFT_MASK are set to 1.
  • (modifiers & ~KeyEvent.SHIFT_MASK) -> every 1-bit in modifiers that "does not belong" to KeyEvent.SHIFT_MASK
  • if ((modifiers & ~KeyEvent.SHIFT_MASK) != 0) -> if there was at least one other bit set to 1 besides KeyEvent.SHIFT_MASK do something...
9
ответ дан 24 November 2019 в 12:39
поделиться

From the official docs http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op3.html:

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

3
ответ дан 24 November 2019 в 12:39
поделиться
Другие вопросы по тегам:

Похожие вопросы: