Thread: Bit fields

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    Bit fields

    Code:
    int main()
    {
    	struct personal
    	{
    		signed x : 2;
    	}emp;
        emp.x=-3;
        printf("%d\n",emp.x);
    }
    the above code outputs 1 and the code below would output -1. Can someone please explain me the difference in output and how -3 gets stored in 2 bits. I am familiar with the concept of 2's complement.

    Code:
    int main()
    {
    	struct personal
    	{
    		signed x : 2;
    	}emp;
        emp.x=3;
        printf("%d\n",emp.x);
    }
    Last edited by juice; 12-12-2011 at 09:25 AM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There are only four possible values that can be held in a signed two-bit number. What do you think those four values are?
    Hint: -3 is certainly not one of them!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    In the first example you are assigning the last two bits of ...111101 (-3) to 2 bits. That means you end up with 01 = 1.
    In the second example you are assigning the last two bits of ...0000011 (+3) to 2 bits. That means you end up with 11 = -1.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by nonoob View Post
    In the first example you are assigning the last two bits of ...111101 (-3) to 2 bits. .
    How did you get ....111101 for -3....
    Isn't it supposed to be 111....111 for machines which copy the sign bit to all the vacant bits, and 100.....011 for machines which don't...

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Besides, we are only using 2 bits, out of which one is supposed to be sign bit..

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by iMalc View Post
    There are only four possible values that can be held in a signed two-bit number. What do you think those four values are?
    Hint: -3 is certainly not one of them!
    ...are they 0, -1, +1 and -2...

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by juice View Post
    ...are they 0, -1, +1 and -2...
    Yep, well done.
    You do indeed appear to be familiar enough with two's-complement.
    I guess everything has been answered then.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by juice View Post
    How did you get ....111101 for -3....
    Isn't it supposed to be 111....111 for machines which copy the sign bit to all the vacant bits, and 100.....011 for machines which don't...
    You need to review 2's complement.

    ...000000 = 0
    ...111111 = -1
    ...111110 = -2
    ...111101 = -3

    That last two bits of -3 are 01.
    Last edited by nonoob; 12-13-2011 at 08:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit fields
    By Edelweiss in forum C Programming
    Replies: 5
    Last Post: 08-23-2011, 10:01 AM
  2. Bit fields
    By Perspektyva in forum C++ Programming
    Replies: 13
    Last Post: 11-22-2008, 02:38 PM
  3. bit fields before non-bit fields . . .
    By dwks in forum C Programming
    Replies: 10
    Last Post: 10-13-2005, 02:36 AM
  4. bit-fields
    By kps in forum C Programming
    Replies: 9
    Last Post: 09-25-2002, 08:25 PM
  5. Bit fields
    By GaPe in forum C Programming
    Replies: 8
    Last Post: 01-22-2002, 02:01 PM