c - Bit level operation -


i have been following tutorial on bit level operation. code working on 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 follows:

x & y = 0

x | y = 11

x ^ y = 11

however problem first answer. understood 1 & 0 = 0, 1& 1 = 1, expecting should have received answer of @ least 10 & 1 = 10. because first bit 1 x , first digit 1 y. , second bit x 0 , y bit 0 result should 0. question why did 0 or , xor received 2 bits result.

thank much. understand there few questions posted regarding bit level operation, answer not clarify question.

remember, these binary operators. you've got @ numbers' base 2 bits, not digits, base 10. let's that. i'll use subscripts indicate base each number written in.

first, let's convert numbers base 2.

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

next, must remember work right-to-left, not left-to-right. can padding numbers 0's. 37 same 037 in decimal —both thirty-seven—12 same 00012 in binary.

1010 = 10102
0110 = 00012

now perform binary operations. see why 10102 & 00012 equal 00002?


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -