Hello, I have 3 questions about bit fields, namely:

  1. When a bit field is longer than 8 bits (cell size RAM) are the cells allocated for this bit field combined?

  2. When I create a variable, a data type that is less than 8 bits (RAM cell size), for example, a variable of 7 bits, remains 1 free bit, can it be enabled (included) in another bit field or will it be considered occupied?

  3. Can I create variables more than 64 bits by means of the C ++ language, if not, then maybe this is implemented in assembler? Please write in detail. Thank.

  • It seems to me that it is first necessary to determine the terminology and context of why you have such questions. What problem do you want to solve, knowing the answers to these questions? - vladimir_ki
  • There are no problems yet, purely theory. - username76

2 answers 2

  1. Unite
  2. Depends on the compiler.
  3. It is possible and on With ++. You can do any type of data, but operations with them will have to be implemented very tricky. You can make object wrappers for them, implement them in the form of arrays, overload operators, and perform cunning operations with them inside.
  • 2. compiler MinGw (g ++), is it worth suffering with variables up to 8 bits or not? - username76
  • Never worked with bitfields. Experiment. - skegg
  • I have little idea how to make such an experiment. - username76
  • > Is it worth suffering with variables up to 8 bits? Watching why. The gain in memory will be minimal, the access speed is most likely lower. In everyday tasks (unless of course you are writing an archiver or working with iron) bit fields are rarely used. - insolor
  • That is, bit operations slow down the program? - username76

2. Example from wikipedia:

struct rgb { unsigned char r:2; unsigned char g:3; unsigned char b:3; }; 

All 3 bit fields will be in the same memory location.

3. There is such a thing as long arithmetic . Numbers are actually stored as arrays, corresponding functions are written for arithmetic operations. For most programming languages ​​there are ready-made libraries for working with "long" numbers.

  • "All 3 bit fields will be in the same memory location." 4 bytes long (!) Although it is for gcc. Perhaps another compiler will put them in 1 byte. - avp
  • It looks like ... I fixed it, now everything is in one byte) - insolor
  • It is better for mine to do not the structure but the union, my compiler even allocates 1 byte to an empty structure. - username76
  • And how many bytes do you want for your fields? Less than one in any way. - avp