that returns an AND between integers?

1

What is the logic that follows. I can not understand why this

int a = 0x0c;
a &= 0x05;

return this

4

I understand the hexadecimal numbers only that I do not understand the logic of the AND between integers

    
asked by martor 02.01.2017 в 22:40
source

2 answers

4

If you pass the values to binary

0x0C = 1 1 0 0
0x05 = 0 1 0 1

And you apply the AND logical operator where:

1 AND 1 = 1 
1 AND 0 = 0
0 AND 0 = 0

The result is:

      1 1 0 0
 AND  0 1 0 1 
      ------- 
      0 1 0 0 

That in decimal is = 4 and in hexadecimal = 0x04

    
answered by 02.01.2017 / 22:54
source
0

An expression that uses the assignment operator & = for example x & = y, is equivalent to

  

x = x & and

Your numbers are Hexadecimals representing 12 and 5 respectively

    
answered by 02.01.2017 в 22:47