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
  • 29,154
  • 11
  • 70
  • 102
Keertan
  • 115
  • 9

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
  • 855
  • 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
  • 4,857
  • 1
  • 26
  • 40
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
  • 531
  • 3
  • 13
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,030
  • 13
  • 23