2 questions about bit fields and variable handling

This is a discussion on 2 questions about bit fields and variable handling within the C Programming forums, part of the General Programming Boards category; 1 question: Code: #include <stdio.h> struct foo{ int a:4; }; int main(void){ struct foo s; s.a=10; printf("%d\n",s.a); getchar(); return 0; ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    33

    2 questions about bit fields and variable handling

    1 question:

    Code:
    #include <stdio.h>
    
    struct foo{
           int a:4;
           };
    
    int main(void){
        struct foo s;
        
        s.a=10;
        
        printf("%d\n",s.a);
        getchar();
        
        return 0;
        
    }
    10 in binary is 1010, but this gives me output -6, shouldn't it be -2?

    the forth digit of 10 in binary is 1 that means the result will be <0, the remaining part of this is 010 which is 2, so why does it give me -6 instead of -2?

    2 question:

    Code:
    int x=0;
    int y=++x; // y=1 and x=1
    
    int x=0;
    int y=x++; //y=0 and x=1
    but if we have

    Code:
    y=0;
    x=y==++y;
    why is x here 1??? I mean 0!=1 right? I can't get it

    also
    Code:
    x=y==y++;
    here x is 0?? Why?

    thanks in advance

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    ok i understand why it gives -6 now

    because we have 4 digits and the forth one is 1(<0) then it takes all the digits and changes them from 1010 to 0101 and adds 1 to the end so 0110

    am i right? I still can't figure out the second one though
    Last edited by jackhasf; 01-25-2010 at 05:16 AM.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    1 answer:
    hmmm...

    2 answer:
    Code:
    y=0;
    x=y==++y;
    What happens is this
    1) y += 1
    2) y == y which is 1 == 1, true
    3) x = true (the compiler gives true == 1)

    Code:
    x=y==y++;
    I would guess you would get 1 again. But maybe you compiler does something like
    1) a = y, b = y++
    2) a == b
    3) a = y = 0, b = y++ = 1. Thus a != b
    In the previous one it would do the same, but
    1) y+= 1
    2) a = b
    3) a = y = 1, b = y = 1. Thus a == b
    since ++y will happen first

    The problem with the second is that it could also do
    3) b = y++ = 1, a = y = 1. Thus a == b
    so it is ambiguous
    It could also do
    1) a = y, b = y
    2) y += 1
    3) a == b, true
    or switch 2) with 3) in which case again true

    The reason of a and b is because it needs temporarily variables for the result of the left and right side. The general idea is that you have something like foo() == boo() so temporarily variables are need for results.

    These are ofcourse assumptions. You realise that in a real program you should never use code like that no matter what.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    thanks a lot

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    > What happens is this
    No, that is "may", not "is".
    It may also blow up your computer, because the whole thing is undefined.
    Expressions

    Whether it's the pre or post form doesn't make it any less undefined.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit fields problem
    By johndirect in forum C Programming
    Replies: 2
    Last Post: 10-21-2008, 12:15 PM
  2. Binary data handling
    By maverickbu in forum C Programming
    Replies: 1
    Last Post: 06-26-2007, 02:14 PM
  3. 2 questions about bit fields
    By Bleech in forum C Programming
    Replies: 6
    Last Post: 11-14-2006, 05:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21