Made to Order Software Corporation Logo


Tag Flash Version: 
Used by PushData Action: 
Not available in PushData Action

A signed or unsigned bit field which width does not directly correspond to an existing C type.

In structures, the width of the field is specified after the field name like in C bit fields. In case of Flash, it can be dynamic in which case a variable name is specified.

Signed bit fields have an implied sign extend of the most significant bit of the bit field. So a signed bit field of 2 bits support the following values:

Decimal Binary
-2 10
-1 11
0 00
1 01

All bit fields are always declared from the MSB to the LSB of the bytes read one after another from the input file. In other words, the first bit read in the file correspond to the MSB of the value being read. As a side effect, the bytes in the file appear as if they were defined in big endian order (which is the opposite of the char, short, long, long long, fixed, float, and double declared outside a bit field.)

The following is a slow but working algorithm to read bit fields in C:

	/* global variables (could be in a structure or an object) */
	long	mask, count, last_byte;

	/* call once before to read a bit field to reset the parameters */
	void start_read_bits(void)
		mask = 0x80;
		count = 0;

	/* call for each bit field */
	long read_bits(long bit_size)
		/* NOTE: any bit field value is at most 32 bits */
		/* the result of this function could also be an unsigned long */
		long		result;
		unsigned long	bit;
		bit = 1 << (bit_size - 1);
		while(bit != 0) {
			if(mask == 0x80) {
				last_byte = read_input();
			if(last_byte & mask) {
				result |= bit;
			mask /= 2;
			if(mask == 0) {
				mask = 0x80;
			bit /= 2;

Note that this function is safe but it should certainly check the validity of the input variable. read_input() is expected to return one byte from the input file.


Post new comment

The content of this field is kept private and will not be shown publicly.
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Syndicate content