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 */
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.