5

this is a function for changing bit value of image. what does |= and ^= mean?

private int setBitValue(int n,int location,int bit)  {
    int toggle=(int)Math.pow(2,location),bv=getBitValue(n,location);
    if(bv==bit)
       return n;
    if(bv==0 && bit==1)
       n|=toggle;        // what does it do?
    else if(bv==1 && bit==0)
       n^=toggle;        // what does it do?

    return n;
}
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
Keertan
  • 115
  • 10

4 Answers4

4

Its the same short form as in +=

n |= toogle

is the same as

n = n | toogle

the | is here the binary-or operator and ^ is the binary xor-operator

Mikescher
  • 875
  • 2
  • 16
  • 35
2

They are short hand assignment operations.

n|=toggle;       is equivalent to           n=n|toggle;

and

n^=toggle;       is equivalent to           n=n^toggle;

And

| is bitwise OR    
^ is bitwise XOR
4J41
  • 5,005
  • 1
  • 29
  • 41
1

They're the bitwise OR equals and the bitwise XOR equals operators. They are mainly used for dealing with bit flags. I highly recommend this article if you want to learn more about bitwise and bit-shifting operations.

Someone
  • 551
  • 3
  • 14
0

These are shorthand bitwise operators. Like += using |= is the same as:

a = a | b;

Read the Oracle Documentation about Bitwise and Bit Shift Operators for further information.

markusthoemmes
  • 3,080
  • 14
  • 23