uint8_t data types initialisation [duplicate] - c

This question already has answers here:
What does 'unsigned temp:3' in a struct or union mean? [duplicate]
(4 answers)
Closed 5 years ago.
Recently I stumbled upon a code written like this:
typedef struct
{
uint8_t TC0_WG0 :2;
uint8_t TC0_CS :3;
} Timer0;
What I wanted to know is what does the part that says :2; & :3; specifically mean? Is it accessing the bits 0, 1, 2 only or 0, 1, 2 & 3 only of the 8-bit unsigned character or what ?

These are basically bitfields explicitly telling that the TC0_CS would be in 3 bit.
These can be used to save space. In embedded system, I have faced this use when designing an interrupt system. Used bitfield to specify the specific positions as a way to activate deactivate interrupts.
He is not accessing the 0,1 or 2nd bits but OP can using appropriate bit masking.

It is called bit-field members.
cppreference say's :
Bit fields
Declares a member with explicit width, in bits. Adjacent bit field
members may be packed to share and straddle the individual bytes.
A bit field declaration is a struct or union member declaration which
uses the following declarator:
identifier(optional) : width
identifier - the name of the bit field that is being declared. The name is optional: nameless bitfields introduce the specified number of
bits of padding
width - an integer constant expression with a value greater or equal to zero and less or equal the number of bits in the underlying
type. When greater than zero, this is the number of bits that this bit
field will occupy. The value zero is only allowed for nameless
bitfields and has special meaning: it specifies that the next bit
field in the class definition will begin at an allocation unit's
boundary.

Related

Detecting at least one bit is set in collection of bitfields [duplicate]

This question already has answers here:
Test for all bit fileds in C at once
(2 answers)
Closed 6 years ago.
In my program I have a C bit-field structure as:
typedef struct
{
char a:1;
char b:1;
char c:1;
char d:1;
}_OpModes;
_OpModes Operation;
Now I want to check at least one of the flags is set in the above structure, If so do some operation otherwise return.
Though I can do this by checking bit by bit, that will be processor intensive for my Embedded application as structure of flags is big enough. I am looking for some operations such as (operation & 0xFF) to detect.
So can anyone suggest how to do this in C??
There's no formally legal way to do it in one shot.
This is actually a situation in which "manual" implementation of bit-fields (i.e one multi-bit value with access through bitwise operators) is far superior to the language-level declaration of separate 1-bit bit-fields.
Even if you use language-level bit-fields, it is still a good idea to clump closely related flags together onto one larger bit-field instead of separate 1-bit fields (that is flags that have related semantics and thus might have to be processed together). E.g. in your case
#define FLAG_A 0x1
#define FLAG_B 0x2
#define FLAG_C 0x4
#define FLAG_D 0x8
typedef struct
{
unsigned char abcd : 4;
} _OpModes;
If course, if that abcd is the only field in the struct, there's no real need to use a bit-field at all. Bit-fields exist for packing data, and if there's nothing there to pack, there's no need for bit-fields.
And prefer to use unsigned types for bit-fields unless you have a good reason to use a signed one. In your case, for signed char bit-fields you will end up with "flag" fields with values of 0 and -1. It can be made to work, but still looks weird.

How do I create a 3 bit variable as datatype in C? [duplicate]

This question already has answers here:
Is it possible to create a data type of length one bit in C
(8 answers)
Closed 7 years ago.
I can typedef char to CHAR1 which is 8 bits.
But how can I make 3 bit variable as datatype?
You might want to do something similar to the following:
struct
{
.
.
.
unsigned int fieldof3bits : 3;
.
.
.
} newdatatypename;
In this case, the fieldof3bits takes up 3 bits in the structure (based upon how you define everything else, the size of structure might vary though).
This usage is something called a bit field.
From Wikipedia:
A bit field is a term used in computer programming to store multiple, logical, neighboring bits, where each of the sets of bits, and single bits can be addressed. A bit field is most commonly used to represent integral types of known, fixed bit-width.
It seems you're asking for bitfields https://en.wikipedia.org/wiki/Bit_field
Just be aware that for some cases it's can be safer just use char or unsigned char instead of bits (compiler specific, physical memory layout etc.)
Happy coding!
typedef struct {
int a:3;
}hello;
It is only possible when it is inside structure else it is not

Handling bit arrays in C without padding

I'm writing a C program that runs on the Altera NIOS II processor. The program has to interface to a VHDL module on an FPGA test board through a specific memory location. My interface is provided through a Macro, which specifies a base memory address. The VHDL programmer has allocated 32-bits of memory off of that base address, which I'm to fill with binary data separated into four "elements", i.e. [0-11|12-15|16-23|24-31].
My question is, what is the best way to handle these array "elements" as separate data types. I'd like to declare the entire array as a structure to handle the data and declare the different fields using bit-fields, but it's my understanding that this will introduce padding into the 32 bit array.
it's my understanding that [using bit fields] will introduce padding into the 32 bit array
Using bit fields will not introduce padding, unless you explicitly request it: language standard prohibits the compier from padding in between bit fields:
C99 Standard, section 6.7.2.1.10: If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is implementation-defined. The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined.
You can force padding to happen by specifying a bit field of zero width, like this:
struct hw_reg {
int a:10;
int :0; // Yes, this is legal.
int b:6;
};
In your case, sufficient space remains after the first 12 bits to allocate the next four, so there will be no padding. If you needed to split the register differently (say, 12-5-7-8), the use of padding would be implementation-defined.
binary data separated into four "elements", i.e.
[0-11|12-15|16-23|24-31].
I'd try as
struct vhdl_data {
uint32_t a : 12; // bits 0-11
uint32_t b : 4; // bits 12-15
uint32_t c : 8; // bits 16-23
uint32_t d : 8; // bits 24-31
};

significance of int a : 2 syntax [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does ‘unsigned temp:3’ mean?
I came across one syntax like int variable:4;
can anyone tell me what this syntax means?
struct abc
{
int a;
int b:2;
int c:1;
};`enter code here`
It defines a width of a bitfield in a struct. A bitfield holds an integer value, but its length is restricted to a certain number of bits, and hence it can only hold a restricted range of values.
In the code you posted, in the structure a is a 32-bit integer, b is a 2-bit bitfield and c is a 1-bit bitfield.
It is a bit-field. Instead of storing a full integer for b it only stores 2 bits, so b can have values of -2, -1, 0 and 1. Similarly c can only have values of -1 and 0.
Depending on what version of the compiler you have the sign extension is a little unpredictable, and some systems may present these values as 0, 1 2 and 3 or 0 and 1.
This will also pack the fields into less than an integer, but again, this is in an implementation defined manner, and you would do well not to make assumptions about how much memory will actually be used or the order of the data in memory.

Bit field manipulation disadvantages

I was reading this link
http://dec.bournemouth.ac.uk/staff/awatson/micro/articles/9907feat2.htm
I could not understand this following statements from the link, Please help me understand about this.
The programmer just writes some macros that shift or mask the
appropriate bits to get what is desired. However, if the data involves
longer binary encoded records, the C API runs into a problem. I have,
over the years, seen many lengthy, complex binary records described
with the short or long integer bit-field definition facilities. C
limits these bit fields to subfields of integer-defined variables,
which implies two limitations: first of all, that bit fields may be no
wider, in bits, than the underlying variable; and secondly, that no
bit field should overlap the underlying variable boundaries. Complex
records are usually composed of several contiguous long integers
populated with bit-subfield definitions.
ANSI-compliant compilers are free to impose these size and alignment
restrictions and to specify, in an implementation-dependent but
predictable way, how bit fields are packed into the underlying machine
word structure. Structure memory alignment often isn’t portable, but
bit field memory is even less so.
What i have understood from these statements is that the macros can be used to mask the bits to left or right shift. But i had this doubt in my mind why do they use macros? - I thought by defining it in macros the portability can be established irrespective of 16-bit or 32-bit OS..Is it true?I could not understand the two disadvantages mentioned in the above statement.1.bit fields may be no wider 2.no bit field should overlap the underlying variable boundaries
and the line,
Complex records are usually composed of several contiguous long integers
populated with bit-subfield definitions.
1.bit fields may be no wider
Let's say you want a bitfield that is 200 bits long.
struct my_struct {
int my_field:200; /* Illegal! No integer type has 200 bits --> compile error!
} v;
2.no bit field should overlap the underlying variable boundaries
Let's say you want two 30 bit bitfields and that the compiler uses a 32 bit integer as the underlying variable.
struct my_struct {
unsigned int my_field1:30;
unsigned int my_field2:30; /* Without padding this field will overlap a 32-bit boundary */
} v;
Ususally, the compiler will add padding automatically, generating a struct with the following layout:
struct my_struct {
unsigned int my_field1:30;
:2 /* padding added by the compiler */
unsigned int my_field2:30; /* Without padding this field will overlap a 32-bit boundary */
:2 /* padding added by the compiler */
} v;

Resources