0

actually i tried a program like this

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a=8,b=3;
    a|=b;
    printf("%d",a);
    return 0;
}

i dont understand what happens here a|=b;
i have used || this which is used for or but i dont understand wat happens above

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by VernonDozier
0

The || operator is the "logical or" operator. the | operator is the "bitwise OR" operator. The |= operator combines the bitwise OR operator and the assignment operator. Thus the code above is equivalent to the code below.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a=8,b=3;
    int c = (a | b);
    printf("%d",c);
    return 0;
}

So it boils down to what does a | b do? First, write 8 and 3 in base 2. We have 8 pairs of bits. If either of them are 1, the result is a 1. Otherwise it is a 0.

EDIT -- For some reason, I am posting text and the editor thinks it is malformed code. I can't seem to post my textual write-up of bitwise operation and get the editor to understand that I am not posting code that needs to be parsed. Anyway, google for tutorials of "C Bitwise OR operator".

http://en.wikipedia.org/wiki/Bitwise_operation#OR

Edited by VernonDozier

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.