What am I doing wrong defining single bit?
-
I am Using GCC running on Linux / Ubuntu gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4) I need / like to have a single bit variable / flag passed to / from SPI hardware. Single bit defined // need for SPI /I2C typedef unsigned char bit : 1; // single bit Implemented char x; static bit b; // defined in generic def header Compiler error - what initializer is missing? In file included from ../src/MODULES/M_SPI/CASPI.h:53:0, from ../src/MODULES/M_SPI/CASPI.cpp:34: /media/os64/Eclipse/eclipse/Workspace/Eclipse_Oxygen_1A/VNA_2/src/MODULES/MODULE_1602/C_GenericTypeDefs.h:77:33: error: expected initializer before ‘:’ token typedef unsigned char bit : 1; // single bit ^ ../src/MODULES/M_SPI/CASPI.cpp:239:1: error: ‘bit’ does not name a type bit C_SPIClass::I2CTX(unsigned char d) { ^ Thanks for any help. Cheers
-
I am Using GCC running on Linux / Ubuntu gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4) I need / like to have a single bit variable / flag passed to / from SPI hardware. Single bit defined // need for SPI /I2C typedef unsigned char bit : 1; // single bit Implemented char x; static bit b; // defined in generic def header Compiler error - what initializer is missing? In file included from ../src/MODULES/M_SPI/CASPI.h:53:0, from ../src/MODULES/M_SPI/CASPI.cpp:34: /media/os64/Eclipse/eclipse/Workspace/Eclipse_Oxygen_1A/VNA_2/src/MODULES/MODULE_1602/C_GenericTypeDefs.h:77:33: error: expected initializer before ‘:’ token typedef unsigned char bit : 1; // single bit ^ ../src/MODULES/M_SPI/CASPI.cpp:239:1: error: ‘bit’ does not name a type bit C_SPIClass::I2CTX(unsigned char d) { ^ Thanks for any help. Cheers
-
I am Using GCC running on Linux / Ubuntu gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.4) I need / like to have a single bit variable / flag passed to / from SPI hardware. Single bit defined // need for SPI /I2C typedef unsigned char bit : 1; // single bit Implemented char x; static bit b; // defined in generic def header Compiler error - what initializer is missing? In file included from ../src/MODULES/M_SPI/CASPI.h:53:0, from ../src/MODULES/M_SPI/CASPI.cpp:34: /media/os64/Eclipse/eclipse/Workspace/Eclipse_Oxygen_1A/VNA_2/src/MODULES/MODULE_1602/C_GenericTypeDefs.h:77:33: error: expected initializer before ‘:’ token typedef unsigned char bit : 1; // single bit ^ ../src/MODULES/M_SPI/CASPI.cpp:239:1: error: ‘bit’ does not name a type bit C_SPIClass::I2CTX(unsigned char d) { ^ Thanks for any help. Cheers
You can only have bit fields in a struct or union, it would be physically stupid to have them outside a struct or union because they couldn't be packed anyhow you might as well pick any normal type. Your somewhat pointless definition (it saves zero memory space) would be
typedef struct {
unsigned bit : 1; // single bit
} BIT;The initialize would be
static BIT bit = { 0 };
or
static BIT bit = {.bit = 0}; // C11 and above standardThe only point to bit fields is to save space or make naming and offsets easier in particular for hardware registers. The other recommendation I make is only define them as sign or unsigned you don't have to assign a normal C type the compiler internally knows how to cast bit fields to do operations on them. Usually what you are doing on hardware registers is assigning the bits to match something like an 8 bit definition
typedef struct __attribute__((__packed__, aligned(1))) {
unsigned Clk : 1; // clk bit
unsigned Bit : 1; // Data bit
unsigned CS : 1; // Chip select
unsigned _reserved: 5; // 5 unused bits
} MYREGISTER;In the above you have 8 bits packed into a byte so it saves space carrying them.
In vino veritas
-
You can only have bit fields in a struct or union, it would be physically stupid to have them outside a struct or union because they couldn't be packed anyhow you might as well pick any normal type. Your somewhat pointless definition (it saves zero memory space) would be
typedef struct {
unsigned bit : 1; // single bit
} BIT;The initialize would be
static BIT bit = { 0 };
or
static BIT bit = {.bit = 0}; // C11 and above standardThe only point to bit fields is to save space or make naming and offsets easier in particular for hardware registers. The other recommendation I make is only define them as sign or unsigned you don't have to assign a normal C type the compiler internally knows how to cast bit fields to do operations on them. Usually what you are doing on hardware registers is assigning the bits to match something like an 8 bit definition
typedef struct __attribute__((__packed__, aligned(1))) {
unsigned Clk : 1; // clk bit
unsigned Bit : 1; // Data bit
unsigned CS : 1; // Chip select
unsigned _reserved: 5; // 5 unused bits
} MYREGISTER;In the above you have 8 bits packed into a byte so it saves space carrying them.
In vino veritas