4

Hi I want to declare a 12 bit variable in C or any "unconventional" size variable (a variable that is not in the order of 2^n). how would I do that. I looked everywhere and I couldn't find anything. If that is not possible how would you go about saving certain data in its own variable.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Igal Flegmann
  • 582
  • 1
  • 8
  • 19

3 Answers3

4

Use a bitfield:

struct {
   unsigned int twelve_bits: 12;
} wrapper;
Davislor
  • 14,674
  • 2
  • 34
  • 49
4

Unlike Ada, C has no way to specify types with a limited range of values. C relies on predefined types with implementation defined characteristics, but with certain guarantees:

  • Types short and int are guaranteed by the standard to hold at least 16 bits, you can use either one to hold your 12 bit values, signed or unsigned.

  • Similarly, type long is guaranteed to hold at least 32 bits and type long long at least 64 bits. Choose the type that is large enough for your purpose.

  • Types int8_t, int16_t, int32_t, int64_t and their unsigned counterparts defined in <stdint.h> have more precise semantics but might not be available on all systems. Types int_least8_t, int_least16_t, int_least32_t and int_least64_t are guaranteed to be available, as well as similar int_fastXX_t types, but they are not used very often, probably because the names are somewhat cumbersome.

  • Finally, you can use bit-fields for any bit counts from 1 to 64, but these are only available as struct members. bit-fields of size one should be declared as unsigned.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • The `intNleast_t` and `intNfast_t` and their unsigned counterparts are guaranteed to be available. – too honest for this site Jan 18 '16 at 02:26
  • @Olaf: I edited the answer for completeness, but these names are obviously not very friendly. – chqrlie Jan 18 '16 at 02:41
  • Agreed about their names; they are just too long. But still they exist. Anyway - to me, it is not clear what OP actually wants to accomplish and mean by _declare_ here. The available variable sizes can be found very easily. – too honest for this site Jan 18 '16 at 02:49
  • @Olaf: maybe a side effect of some Misra-like coding convention that states *variables should be defined with the smallest type that accommodates their value set*. A counterproductive constraint in my opinion. – chqrlie Jan 18 '16 at 02:55
  • 1
    Hmm... I was lucky until now not having to conform to such stuff (MISRA is a good reading, but a bad idea just to blindly follow). That rule especially can increase code-size because of required sign/zero extension code. I also learned to avoid the standard types (`int`, etc.=. But the older I get the more I think they might actually not that bad to be used if the guaranteed minimum range is sufficient. Of course, if you need a specific size, `(u)intN_t` are the logical choice. – too honest for this site Jan 18 '16 at 03:03
2

Data is always stored in groups of bytes (8 bits each).

In C, variables can be declared of 1 byte (a "char" or 8 bits), 2 bytes (a "short" int on many computers is 16 bits), and 4 bytes (a "long" int on many computers is 32 bits).

On a more advanced level, you are looking for "bitfields".

See this perhaps: bitfield discussion

Community
  • 1
  • 1
  • 2
    1) A byte is not necessarily 8 bits. 2) C does not require octets either. A `char` is the same a s byte, but - see 1 3) Integer types may include padding bits, thus in principle an `int` with 17 used bits is possible. It may or may not contain padding, this depends on the width of a byte/`char`.. – too honest for this site Jan 18 '16 at 02:25
  • If you comment on my comment, use `@`, otherwise your comment can get through unnoticed. About the text: What does please you? If you think I am wrong, provide a reference to the corresponding section in the standard. – too honest for this site Jan 18 '16 at 02:53
  • @Olaf so what are the top 3 selling systems that implement non-8-bit bytes??? :-) – Warren Stephens Jan 18 '16 at 21:05
  • You have any knowledge about DSPs and custom CPUs in FPGAs? There are quite some which have 16 or even 24 bit words only. That is not a matter of quantity. C is a standardised language and that standard is very clear about the terms. – too honest for this site Jan 18 '16 at 21:27
  • @Olaf My understanding is that FPGAs are programmed in HDL. But rather writing for them in (c-like) OpenCL I suppose proves your esoteric point. Someone who is _asking_ about unconventional bit-widths is unlikely to know anything about FPGAs. – Warren Stephens Jan 18 '16 at 21:52
  • I wrote about "custom CPUs in FPGAs". There are some IP cores which do not necessarily have 8 bit addressable unit size, but can be configured (within some range), according to the application. Anyway, it is enough to stick to the C standard - which you apparently try to distract from. – too honest for this site Jan 18 '16 at 21:56