GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

What is the meaning of |= ? I have given a c++ code containing |= below.

#include <cstdio>
using namespace std;

int main() {
    int N, i;
    long long A, B, answer;
    scanf("%d", &N);
    while (N--) {
        scanf("%lld%lld", &A, &B);
        answer = 0;
        for (i = 0; i < 32; ++i) {
            long long k = (A + (1ll << i)) >> i;
            long long P = (k << i) - 1;
            if (P >= B) {
                if ((k & 1) == 0) answer |= 1ll << i;
            }
        }
        printf("%lld\n", answer);
    }
    return 0;
}
asked Aug 21, 2016 by satish mhetre

4 Answers

It's actually, bit wise OR operator.

#include<cstdio>
int main()
{
   int x=5,y=4;

   x |= y;
   /*   x = x OR Y ;
        Binary of x = 101;
        Binary of y = 100;
        SO, (101) OR (100) = 101
        Hence, x = 101 (in Binary).
    */

   printf("%d",x); //prints decimal value of 101 i.e. 5
}

 

answered Aug 22, 2016 by Mazhar MIK

It is a bitwise "or equal" so for example.

char x = 3, y = 2;

x |= y

this would take x which in bits is 0000011

and would take y which in bits is 0000010 and would perform and "or" operator on them and set x equal to the results

                      0000011

                      0000010

would equal  0000001

so X would equal 00000001 which in base ten is 1

answered Aug 22, 2016 by porterj091

#include<stdio.h>

main()

{

int x=4,y=5;

x  |= y;

printf("x=%d",x);

 

}

answered Aug 22, 2016 by balram1990
Got it. Thanks to all.
answered Aug 22, 2016 by Satish Mhetre
...