RoR
RoR

Reputation: 16472

Bit Manipulation Syntax Question Java

checker |= (1 << val);

What does the |= do?

The 1 << val shifts the bits to the left which increases the value.

Upvotes: 1

Views: 360

Answers (2)

templatetypedef
templatetypedef

Reputation: 372764

This code is equivalent to

checker = checker | (1 << val)

Here, | is the bitwise OR operator, which takes in two numbers and returns a new number with 1 bits set anywhere that either input number has a one bit set. The |= you're seeing is the "bitwise OR with assignment," which is like += or *= but with |.

Upvotes: 5

EboMike
EboMike

Reputation: 77732

Same as checker = checker | (1 << val), just like checker += val is the same as checker = checker + val.

| means logical OR, i.e. if either source value has a bit set, it will be set in the target.

Upvotes: 5

Related Questions