Home > Back-end >  why does it equal 1?
why does it equal 1?

Time:09-26

#include <iostream>

using namespace std;

int main() {
    int x = 5;
    x &= 3;
    cout << x;
    return 0;
}

CodePudding user response:

5   = 101
3   = 011
5&3 = 001 = 1

& Truth Table:
x y x&y
0 0  0
0 1  0  <<<
1 0  0  <<< these 3 were used above
1 1  1  <<<

CodePudding user response:

I recommend that you read about Bitwise Operators in C . You can visit this link

& is Bitwise AND

Convert 5 and 3 in binary and perform AND operation, 5&3 will give the result as 1.

5   = 101
3   = 011
5&3 = 001 = 1
  •  Tags:  
  • c
  • Related