2

Hi all i have for loop with a ~ operator never found this before on any of the code

    for (int i = 0; i < bytes.length; i++) {
        mashed[i] = (byte) ~bytes[i];

        }

what does the ~ do?

i haven't found anything like this on the internet or before anywhere so maybe someone can help me thanks in advance

  • As an addition to the posted answers: [Bitwise and Bit Shift Operators](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html) and [The Java® Language Specification: 15.15.5. Bitwise Complement Operator ~](http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.15.5) – informatik01 Jun 29 '13 at 15:16

8 Answers8

4

Its an operator of ~ bitwise NOT

The bitwise NOT "~" operator inverts each bit in the operand i.e. this operator changes all the ones to zeros and all the zeros to ones.

All operators

And to know how internally works :How does the bitwise complement (~) operator work?

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
2

It is the bitwise complement operator.

Example:

If the value is 2 (0000 0010), the bitwise complement is 1111 1101

karthikr
  • 97,368
  • 26
  • 197
  • 188
2

From Java's tutorials, http://docs.oracle.com/javase/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".

Pu Gong
  • 176
  • 3
1

It is bitwise operator which will apply not gate on every bit of the data. Eg when data bits are 101, then it will become 010.

Dan Bray
  • 7,242
  • 3
  • 52
  • 70
1

The ~ is for bitwise inversion -- 0s become 1s, 1s become 0s.

jedwards
  • 29,432
  • 3
  • 65
  • 92
1

The ~ operator is bitwise NOT, it inverts the bits in a binary number:

NOT 011100
  = 100011
Mohit Bhansali
  • 1,725
  • 4
  • 20
  • 43
1

From the docs:

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

Zavior
  • 6,412
  • 2
  • 29
  • 38
0

The Tilde (~) does a bitwise compliment of a numerical value.

1011 0011 = ~0100 1100
Alexey
  • 3,607
  • 8
  • 34
  • 54