Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

BitField

srimks
New Contributor II
430 Views
Hello,

Below is Bitfield code -

--------bitfield.c---------
#include

struct
{
int bit1 : 1;
int bit2 : 1;
} bits;

bits.bit1 = 1;
bits.bit2 = 1;

int main()
{
return 0;
}
--

When using ICC-v11.0 on Linux x86_64, I get below messages -

--
bitfield.c(9): warning #77: this declaration has no storage class or type specifier
bits.bit1 = 1;
^

bitfield.c(9): error: declaration is incompatible with "struct bits" (declared at line 7)
bits.bit1 = 1;
^

bitfield.c(9): error: expected a ";"
bits.bit1 = 1;
^

bitfield.c(10): warning #77: this declaration has no storage class or type specifier
bits.bit2 = 1;
^

bitfield.c(10): error: expected a ";"
bits.bit2 = 1;
^

compilation aborted for bitfield.c (code 2)
--
I did try with unsigned too.

~BR
0 Kudos
3 Replies
Judith_W_Intel
Employee
430 Views
The problem is that you can't put assignment statements like "bits.bit1 = 1;" at global scope.
You either need to move them inside main() or initialize the structure using an aggregate initializer
list, i.e.:

struct
{
int bit1 : 1;
int bit2 : 1;
} bits = { 1, 1 };

Judy
0 Kudos
jimdempseyatthecove
Honored Contributor III
430 Views
The problem is that you can't put assignment statements like "bits.bit1 = 1;" at global scope.
You either need to move them inside main() or initialize the structure using an aggregate initializer
list, i.e.:

struct
{
int bit1 : 1;
int bit2 : 1;
} bits = { 1, 1 };

Judy

You also have the additional problem that a 1 bit int will hold values of 0 and -1.
If you want values of 0 and 1 then use "unsigned bit1: 1"

Jim
0 Kudos
srimks
New Contributor II
430 Views
The problem is that you can't put assignment statements like "bits.bit1 = 1;" at global scope.
You either need to move them inside main() or initialize the structure using an aggregate initializer
list, i.e.:

struct
{
int bit1 : 1;
int bit2 : 1;
} bits = { 1, 1 };

Judy
Thanks Judy, it worked.

~BR
0 Kudos
Reply