Search code examples
c++chexobjective-c-category

Not of hexadecimal value 0xffff?


I guess the output should be "0000" but it's ffff as Not of ~ffff->0000 0000 0000 0000

#include<stdio.h>
int main()
{
    unsigned int a=0xffff;
    ~a;
    printf("%x\n", a);
    return 0;
}

Solution

  • As Tim and Vlad said, you aren't doing anything with the bit-wise inversion.

    Even if you change the code to a = ~a;, you may not get zero. That's because if unsigned int has more than 16 bits, you'll have leading zeros, which become 1's after inversion.

    So I expect your output to be

    FFFF0000

    or even

    FFFFFFFFFFFF0000

    If you want 16-bit bitwise operations, you can use

    #include <inttypes.h>
    uint16_t a;