Bitwise AND operation not clear

91 views Asked by At

I'm trying to understand the bitwise AND operator in C; it works until I put a 0 in front of 177.

I'm doing this by hand to make sure I understand what the compiler is doing

#include <stdio.h>

main () {
    printf ("%d\n", 1999 & 177); 
    return 0; 
}

The answer above is 129, this is my answer when I do it by hand as well

#include <stdio.h>

main () {
   printf ("%d\n", 1999 & 0177); 
   return 0; 
}

The compiler above gives me the answer 79, can someone please explain how it gets to the answer 79?

1

There are 1 answers

0
Mahonri Moriancumer On

177 is base 10 (decimal) = 0xB1 = 02618.

0177 is base 8 (octal) = 12710 or 0x7F.

0x177 is base 16 (hexadecimal) = 37510 or 05678.

1999 = 0x07CF = 0000 0111 1100 1111 Binary.
0177 = 0x007F = 0000 0000 0111 1111
              & -------------------
                0000 0000 0100 1111 = 79