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.

3
  • 1
    This is a bit too broad. Could you specify what you are trying to achieve? Commented Jan 18, 2016 at 2:02
  • One way is to declare a 16-bit variable and only use the lowest 12 bits. Commented Jan 18, 2016 at 2:09
  • What do you actually try to accomplish, what is your deeper problem? That might be an XY-problem. Commented Jan 18, 2016 at 2:50

3 Answers 3

4

Use a bitfield:

struct { unsigned int twelve_bits: 12; } wrapper; 
Sign up to request clarification or add additional context in comments.

Comments

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.

5 Comments

The intNleast_t and intNfast_t and their unsigned counterparts are guaranteed to be available.
@Olaf: I edited the answer for completeness, but these names are obviously not very friendly.
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.
@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.
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.
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

6 Comments

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..
If you comment on my comment, use @<name>, 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.
@Olaf so what are the top 3 selling systems that implement non-8-bit bytes??? :-)
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.
@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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.