Thread: stumbled on this:

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    2

    Unhappy stumbled on this:

    Hi. I have a simple question.

    Say I have:

    unsigned int var1 = 5;
    unsigned int var2 = ~(BYTE)var1;

    printf("var2: %u\n", var1); // output: var1: 4294967290

    How is this result possible if var1 is casted to a BYTE and then have it's bits flipped (ignoring the 3 most significant bytes) which thereafter should have resulted in 250 as the output.

    I don't see how var2 = ~(BYTE)var1;

    is diffrent from var2 = (BYTE)~var1; because what's inside parenthesis is done first and then the bitwise NOT operator.


    Help

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Let me try to explain what is happening...
    Here's code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
            unsigned int var1 = 5;
            char tmp;
            tmp = ~(char)var1;
            unsigned int var2 = tmp;
            
            printf("\nvar1 = %u, var2 = %u, tmp = %d",var1,var2,tmp);
            system("pause");
    }
    My output:

    var 1 = 5, var2= 4294967290, tmp = -6

    Why?

    OK, on most machines (32 bit) unsigned it is 4 bytes (you can check with sizeof operator) and number 5 is represented like:

    00000000 00000000 00000000 00000101

    now when you cast it to char (1 byte) you get:

    00000101
    Now when you complement it you get

    11111010

    Since you tmp is defined as signed (default signed on most compilers) this is interpreted in this way:
    first bit is signed bit if it is 1 tthen nubmer is negative,since it's two's complement number is then
    1111010 this must be complemented first
    0000101
    and 1 is added so finaly
    Code:
      0000101
    +0000001
    -------------
      0000110
    So number is -6.

    Now, in line:

    Code:
     unsigned int var2 = tmp;
    you there is a promotion form char to unsigned int.
    Since tmp is interprated as negative number because the most significat bit is 1 it will be promoted like:

    11111111 11111111 11111111 11111010

    and that is var2= 4294967290.

    So, basically your question is about char to int promotion. I strongly recommend you reading FAQ. If you want to get result 250 you'll need to use something like this:
    Code:
     unsigned int var2 = (unsigned char)(~(char)var1);
    Hope this helps.

    - Micko
    Last edited by Micko; 05-05-2005 at 03:28 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Edit boxes and tabs
    By Malek in forum Windows Programming
    Replies: 3
    Last Post: 06-20-2002, 11:50 AM
  2. I stumbled apon this header files site...
    By Silentsharp in forum C++ Programming
    Replies: 0
    Last Post: 02-19-2002, 06:02 PM
  3. I just stumbled upon this...
    By biterman in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-14-2002, 03:55 AM