-4
#include<stdio.h>
int main(void)
{
struct str
{
    int i: 1;
    int j: 2;
    int k: 3;
    int l: 4;
};

struct str s;

s.i = 1;
s.j = 2;
s.k = 5;
s.l = 10;

printf(" i: %d \n j: %d \n k: %d \n l: %d \n", s.i, s.j, s.k, s.l);

getchar();
return 0;
}

Output Given in the website:

i: 1
j: 2
k: 5
l: 10

I got

i :-1 
j : -2 
k : -3
l : -6

I don't understand what is bit field. And can someone tell me what's going on in this code here?

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
  • 2
    Read your book or google or search this site to find out what a bitfield is. And your results differ from the example because most of the behaviour of bitfields is not standardized. So it is a good idea to avoid bitfields if you want your code to work on different or future compilers – M.M Jan 08 '17 at 06:47
  • 2
    *"Output Given in the website"* - What website? – StoryTeller - Unslander Monica Jan 08 '17 at 06:47
  • 1
    `int i:1` makes zero sense. A signed-int bit field reserves a bit for the sign, and since you've only provided one bit, you're left with a signed-int bit field that can be negative or positive, but have no redeemable magnitude value (there are no bits left for a value). The rest of this code seems to make it a practice to breach the maximum value capable in the provided bits (less the sign-bit). – WhozCraig Jan 08 '17 at 07:22
  • `I don't understand what is bit field` if you don't know what it is why don't read a book and/or google for it? SO is not where you would learn basic programming – phuclv Jan 08 '17 at 08:08
  • Possible duplicate of [What is a bit field in layman's terms?](http://stackoverflow.com/questions/19627169/what-is-a-bit-field-in-laymans-terms) – phuclv Jan 08 '17 at 08:10
  • @WhozCraig `int i: 1` is implementation-defined whether signed or unsigned. But if it is signed and 2's complement then it can hold values `0` or `-1` (the sign bit has value) – M.M Jan 08 '17 at 09:23
  • @M.M Yeah, I always forget the bit itself retains the negation or lack thereof. Thanks for the reminder. – WhozCraig Jan 08 '17 at 10:20

1 Answers1

2

If you write down a column of the possible bit patterns for each width of those signed variables, with the decimal equivalent next to them, you will soon see why you get those results. Assuming 2's complement respresentation, a 1-bit signed variable has two possible values: -1 and 0. A 2-bit signed variable stores values in the range -2 to 1 and so on.

Bin Dec     Bin Dec     Bin Dec
011  3      01   1      0    0
010  2      00   0      1   -1
001  1      11  -1
000  0      10  -2
111 -1
110 -2
101 -3
100 -4

In your case of s.i = 1; the int value 1 is binary 1, and as you can see for the 1-bit variable, this has the value -1.

In your case of s.j = 2; the int value 2 is binary 10, and as you can see for the 2-bit variable, this has the value -2.

In your case of s.k = 5; the int value 5 is binary 101, and as you can see for the 3-bit variable, this has the value -3.

However this is just an explanation of what might be happening, because it is implementation-defined behaviour to assign a value which is outside of the representable range of a signed integer.

I will leave you to work out the 4-bit one for yourself. With s.l = 10; that value is also outside of the range of the 4-bit signed variable, which is -8 to 7.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56