0

I am have been following a tutorial on bit level operation. the code that i was working on is as follows:

    int main(void){

    puts("bit-level calculations:");
    puts("----------------------");



    unsigned int x = 10;
    unsigned int y = 1;
    unsigned int result;


    result = x&y;
    printf("x & y = %d\n", result);

    result = x|y;
    printf("x | y = %d\n", result);

    result = x^y;
    printf("x ^ y = %d\n", result);



   }

the result was as follows:

x & y = 0

x | y = 11

x ^ y = 11

however my problem is with the first answer. what i understood is the 1 & 0 = 0, but 1& 1 = 1, what i was expecting was that i should have received an answer of at least 10 & 1 = 10. Because the first bit is 1 for the x and the first digit is 1 for the y. and the second bit for x is 0 and y bit is 0 so the result should be 0. the question is why did i get only zero where for the or and Xor i received two bits as a result.

thank you very much. i do understand that there are a few questions that was posted regarding the bit level operation, however the answer does not clarify my question.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 4
    You are confusing binary and decimal. `10` is decimal. In binary it is `1010`. – kaylum Jun 10 '16 at 22:23
  • @Kaylum: In what world is binary 10 = decimal 1010? I think you might be the one that confused decimal and binary. :-) Decimal 10 = binary 1010, not the other way around. – Ken White Jun 10 '16 at 22:25
  • @KenWhite Err, confusion reigns :-) Thanks for pointing out my fat finger. – kaylum Jun 10 '16 at 22:27
  • Here's a interactive tool: http://www.ambrsoft.com/MathCalc/BitP/bitPlay.htm – jarmod Jun 10 '16 at 22:32

4 Answers4

2

Remember, these are binary operators. You've got to look at the numbers' base 2 bits, not their digits, which are base 10. Let's do that. I'll use subscripts to indicate what base each number is written in.

First, let's convert the numbers to base 2.

1010 = 10102     (ten in base 10 is "1010" in base 2)
110 = 12             (in either base, one is written as "1")

Next, we must remember to work right-to-left, not left-to-right. We can do that by padding the numbers with 0's. Just as 37 is the same as 037 in decimal —both are thirty-seven—12 is the same as 00012 in binary.

1010 = 10102
0110 = 00012

Now perform the binary operations. Do you see now why 10102 & 00012 is equal to 00002?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Thanks to all of you, i believe sg7 answer was right as well, however what i loved in John Kugelman answer is that it was more clear and step by step. as i said thanks to all of you guys i am quit sure you have all been so helpful. if only i had a choice to tick on all the answers i would have done so. – Jamal ALharthy Jun 10 '16 at 22:38
1

Always consider radix when doing bitwise operations. It defaults to decimal in your program.

1010 and 0xa and 012 and 0b1010 and 10102, all are notations for the same thing.

That is, just as your decimal 1010 is 1 * 10 + 0, it's also 1 * 23 + 21 or perhaps, mixing radices, 8 + 2, so in binary, 010102.

When you combine it via operators with 1 (which, mercifully, is the same in every radix) you are matching up with the rightmost 0 in your 1010.

DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
0

Binary representation of 10 and 1 are as follows

10 = 1010

1 = 0001

So:

10 & 1

= 1010 & 0001

= 0000

sg7
  • 6,108
  • 2
  • 32
  • 40
0

In some versions of gcc and other compilers, you can use literal binary values, i.e.

 unsigned int x = 0b10;  
 unsigned int y = 0b01;

Then it will work the way you want it to.

Community
  • 1
  • 1
Mark Lakata
  • 19,989
  • 5
  • 106
  • 123