views:

311

answers:

7

Can someone please explain why this program outputs 0x00000004?

class AndAssignment
{
    static void Main()
    {
        int a = 0x0c;
        a &= 0x06;
        Console.WriteLine("0x{0:x8}", a);
    }
}
/*
Output:
0x00000004 

*/
+14  A: 

0x0c = 1100 in binary
0x06 = 0110 in binary
& operation is a binary AND which sets a bit to 1 if it's set in both operands, so:
0x0c & 0x06 = 1100 & 0110 = 0100 = 0x04
You can use windows calculator to see how integers is presented in different forms (hex and binary in your case). More info.

Dmitriy Matveev
thank you so much.
A: 

thank you for your answer but how did you understand 0x0c = 1100

or 0100 = 0x04

It's just math, the place-value system. The number c in base 16 is 1100 in base 2, or 12 in base 10. Try looking up a tutorial on hexadecimal and binary numbers.
David Zaslavsky
He used the windows Calculator. 0x denotes hexidecimal (Hex) ... therefore put the calculator in hex mode and type in "0c" then change to binary mode (Bin) and the 0c will change to 1100 ... c is hexidecimal for the number 12 and 1100 is binary for the number 12 also...
Sam
0x is just to tell computer that the number that follows is hexadecimal.
PolyThinker
0x just shows you that the number is hexidecimal it has no effect on the number it self. Otherwise if I write 300 you don't know if I mean hex or decimal and if it's used wrong you could get the wrong output but if I do 0x300 you can see that I mean hex which = 768
Nathan W
thank you all .I understand. It was very helpful, I really appritiate. Sara
A: 

Maybe this article at Wikipedia helps you understand.

Peter
Thank you so much.
A: 

You have to know the basics of converting from number bases. Decimals are base 10. Binary is base 2. Hexadecimal is base 16.

Look at the following table for hexadecimal:

16^0 = 1
16^1 = 16
16^2 = 256
16^3 = 4096

Hexadecimals have the following numbers: 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f.

so you have: 0C in hex, or just C.

16^0 x c (or 12) = 12 in decimal.
16^1 x 0 = 0

convert 12 decimal into binary now. Im just gonna show you simple addition pattern for small numbers:

2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16

so to make 12 in binary, you need one group of (2^3) and one group of (2^2). Therefore you have

1100.

If you convert it to decimal just like you did with hex, you'll end up with 12.

0 x 2^0 = 0
0 x 2^1 = 0
1 x 2^2 = 4
2 x 2^3 = 8

total = 12.
masfenix
A: 

Sara, he's converting between hexadecimal and binary. Have a read of this:
http://www.purplemath.com/modules/numbbase.htm

C (in hex) = 12 (in base ten) 1100 (in binary) = 12 (in base ten)

gkrogers
A: 

that is basic binary math.

+1  A: 

also, windows calculator can do a wonderful job at converting between hex/bin/oct and decimal values