Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsigned char c = 255 is "11111111" or not?

Tags:

c

char

binary

Here is my problem code:

#include "stdio.h"

int main()
{
        char a = -1;
        unsigned char b = 255;
        unsigned char c = 0;
        if((~a) ==  c)
                printf("OK 1");
        else
                printf("bad 1");
        printf("\n");
        if((~b) ==  c)
                printf("OK 2");
        else
                printf("bad 2");
        printf("\n");
}

I expected this to print:

OK 1

OK 2

But, I get OK 1 and bad 2!

If unsigned char b is 255 (11111111), then ~b should be 00000000. Why does it not equal c?

I work on Linux SUSE, using gcc.

like image 290
Lidong Guo Avatar asked Jul 25 '13 22:07

Lidong Guo


2 Answers

You're getting bitten by integer promotions. When you do:

~b == c

b and c both get promoted to int. That means you're really doing:

~0x000000ff == 0

That ends up comparing:

0xffffff00 == 0

Which don't match. It works in your first case because your char type is signed, and gets sign extended in the promotion:

~a == c
~(-1) == 0
~0xffffffff == 0
0 == 0
like image 66
Carl Norum Avatar answered Sep 30 '22 12:09

Carl Norum


Because of standard integral promotions: In the expression ~b, the operand is promoted to int, which could be something like 0x000000FF; the result is the integer 0xFFFFFF00.

You have to convert the result back to unsigned char:

if ((unsigned char)(~b) == c) /* ... */
like image 29
Kerrek SB Avatar answered Sep 30 '22 12:09

Kerrek SB