C Bit Fields

C Bit Fields


C Bit Fields: Full example code

22 February 2017  in  tagged bit fields by 


The following two examples demonstrate the use of bit fields to reduce memory consumption of certain applications.

In the first example we create a compressed ‘bit’ struct and on the second we create a weird struct representation for bytes to show that the size of that struct is significantly less that the original one.

First example: Using bit fields to create a ‘bit’ structure

 bits_bit_fields.c (compressed) (265 downloads)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

#include <stdio.h>

 

// Weird structure to represent a 'bit'

typedef struct {

    unsigned char value;

} bit;

 

// 'bit' structure using bit fields

typedef struct {

    unsigned char value : 1;

} bit_bit_field;

 

int main( ) {

 

    printf"Memory size occupied by 'bit' struct : %zu bytes\n"sizeof(bit));

    printf"Memory size occupied by 'bit_bit_field' struct : %zu bytes\n"sizeof(bit_bit_field));

 

    bit bits[8];

    bit_bit_field bits_bit_field[8];

 

    printf"Memory size occupied by 'bits' array : %zu bytes\n"sizeof(bits));

    printf"Memory size occupied by 'bits_bit_field' array : %zu bytes\n"sizeof(bits_bit_field));

 

    // Setting the value of the first 'bit' and then printing it.

    // We will use various values for this test to show that when you set a value to a bit field

    // that is greater that the allowed size it will fill it using the last bits only.

    // It will not spill data though to neighbouring 'bits'.

    unsigned char value;

    for (value = 0; value < 4; value++)

    {

        printf("Input Value: %d\n", value);

        int bits_i;

        const int bits_length = (sizeof(bits) / sizeof(bit));

        for (bits_i = 0; bits_i < bits_length; bits_i++)

        {

            if (bits_i % 2)

            {

                bits[bits_i].value = 0;

            }

            else

            {

                bits[bits_i].value = value;

            }

            printf("%d", bits[bits_i].value);

        }

        printf("\n");

 

        int bits_bit_field_i;

        const int bits_bit_field_length = (sizeof(bits_bit_field) / sizeof(bit_bit_field));

        for (bits_bit_field_i = 0; bits_bit_field_i < bits_bit_field_length; bits_bit_field_i++)

        {

            if (bits_bit_field_i % 2)

            {

                bits_bit_field[bits_bit_field_i].value = 0;

            }

            else

            {

                bits_bit_field[bits_bit_field_i].value = value;

            }

            printf("%d", bits_bit_field[bits_bit_field_i].value);

        }

        printf("\n");

    }

    return 0;

}

bits_bit_fields.c (compressed) (265 downloads)

Execution output

Memory size occupied by 'bit' struct : 1 bytes
Memory size occupied by 'bit_bit_field' struct : 1 bytes
Memory size occupied by 'bits' array : 8 bytes
Memory size occupied by 'bits_bit_field' array : 8 bytes
Input Value: 0
00000000
00000000
Input Value: 1
10101010
10101010
Input Value: 2
20202020
00000000
Input Value: 3
30303030
10101010

Second example: Using bit fields to create a ‘byte’ structure where each ‘bit’ is another named member

 bytes_bit_fields.c (compressed) (262 downloads)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

#include <stdio.h>

 

// Weird structure to represent a 'byte'

typedef struct {

    unsigned char bit_0;

    unsigned char bit_1;

    unsigned char bit_2;

    unsigned char bit_3;

    unsigned char bit_4;

    unsigned char bit_5;

    unsigned char bit_6;

    unsigned char bit_7;

} byte;

 

// 'byte' structure using bit fields

// Unfortunately we cannot declare an array where the values are bit fields,

// so we have to declare each member separately.

// We instruct the compiler to use only one bit per element.

typedef struct {

    unsigned char bit_0 : 1;

    unsigned char bit_1 : 1;

    unsigned char bit_2 : 1;

    unsigned char bit_3 : 1;

    unsigned char bit_4 : 1;

    unsigned char bit_5 : 1;

    unsigned char bit_6 : 1;

    unsigned char bit_7 : 1;

} byte_bit_field;

 

int main( ) {

 

    printf"Memory size occupied by 'byte' struct : %zu bytes\n"sizeof(byte));

    printf"Memory size occupied by 'byte_bit_field' struct : %zu bytes\n"sizeof(byte_bit_field));

 

    byte bytes[8];

    byte_bit_field bytes_bit_field[8];

 

    printf"Memory size occupied by 'bytes' array : %zu bytes\n"sizeof(bytes));

    printf"Memory size occupied by 'bytes_bit_field' array : %zu bytes\n"sizeof(bytes_bit_field));

 

    // Setting the value of the first 'bit' and then printing it.

    // We will use various values for this test to show that when you set a value to a bit field

    // that is greater that the allowed size it will fill it using the last bits only.

    // It will not spill data though to neighbouring 'bits'.

    unsigned char value;

    for (value = 0; value < 4; value++)

    {

        printf("Input Value: %d\n", value);

        int bytes_i;

        const int bytes_length = (sizeof(bytes) / sizeof(byte));

        for (bytes_i = 0; bytes_i < bytes_length; bytes_i++)

        {

            if (bytes_i % 2)

            {

                bytes[bytes_i].bit_3 = 0;

            }

            else

            {

                bytes[bytes_i].bit_3 = value;

            }

            printf(" %d  ", bytes[bytes_i].bit_3);

        }

        printf("\n");

 

        int bytes_bit_field_i;

        const int bytes_bit_field_length = (sizeof(bytes_bit_field) / sizeof(byte_bit_field));

        for (bytes_bit_field_i = 0; bytes_bit_field_i < bytes_bit_field_length; bytes_bit_field_i++)

        {

            if (bytes_bit_field_i % 2)

            {

                bytes_bit_field[bytes_bit_field_i].bit_3 = 0;

            }

            else

            {

                bytes_bit_field[bytes_bit_field_i].bit_3 = value;

            }

            printf("%d%d%d ",

                   bytes_bit_field[bytes_bit_field_i].bit_2,

                   bytes_bit_field[bytes_bit_field_i].bit_3,

                   bytes_bit_field[bytes_bit_field_i].bit_4);

        }

        printf("\n");

    }

    return 0;

}

bytes_bit_fields.c (compressed) (262 downloads)

Execution output

Memory size occupied by 'byte' struct : 8 bytes
Memory size occupied by 'byte_bit_field' struct : 1 bytes
Memory size occupied by 'bytes' array : 64 bytes
Memory size occupied by 'bytes_bit_field' array : 8 bytes
Input Value: 0
 0   0   0   0   0   0   0   0  
000 000 000 000 000 000 000 000 
Input Value: 1
 1   0   1   0   1   0   1   0  
010 000 010 000 010 000 010 000 
Input Value: 2
 2   0   2   0   2   0   2   0  
000 000 000 000 000 000 000 000 
Input Value: 3
 3   0   3   0   3   0   3   0  
010 000 010 000 010 000 010 000

Share this:

C/C++: Full example using a linked list with custom struct

9 December 2016

In "C"

C: Split a buffer to a list of segments of a specific size in bits

The following code will split a buffer in C to a list of segments. The size of the segments does not have to be a multiple of a byte. User defines the size of the segments in bits when calling node_t *segment(const unsigned char buffer[], const unsigned int buffer_bytes_size, const…

18 March 2016

In "C"

Reading data from /dev/i2c-%d

The following code will read a byte from position 0x10, of the register at 0x3f of the device /dev/i2c-2. To compile this code, you need the helper library i2c-dev.h which can be found in the download package here: main.c linux/i2c-dev.h

12 March 2017

In "C"

This post is also available in: Greek

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Post navigation

Social links

Languages

Google Search

Most Recent Posts

Categories:

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Email Address

Subscribe

Languages

Support this work

 

Tags

2017 address applications audio awk bash C++ CentOS crystalxp crystalxp.net cut cyprus extract fedora ffmpeg find git GNU/Linux google grep hash hash code howtos ieee ieeextreme Informatics java Latex limassol linux map mysql nicosia Olympiad programming server ssh telephone telephone number tux tux.crystalxp.net ubuntu video wifi wordpress

Pages

Google Search

Archives

 

:)