2

Does C language allow us to choose how many bits to reserve for a variable? For example, if we create whole number variable which we will use as bool (1=true, 0=false) we need only one bit. How can I reserve only one bit for that? Is it possible?

sofia.bul
  • 219
  • 2
  • 6
  • 1
    No. The smalles addressable unit is a `char`. But you can pack multiple `bool` into a bitfield-struct. – too honest for this site Oct 27 '15 at 23:27
  • if using a cpu that has bit addressable data, like the 8051 family, then yes, however for most CPUs, the smallest is one byte (usually 8 bits) however, something like this: int bits { unsigned n31 :1; .... unsigned n0 "1; }; will enable modifying single bits (however the actual assembly with (with most CPUs) perform a read/modify/write of the whole int variable. – user3629249 Oct 27 '15 at 23:30
  • Can you explain why you want to do this? Could this be an [XY problem](http://meta.stackexchange.com/q/66377/167210)? – Keith Thompson Oct 27 '15 at 23:32
  • I'm just trying to see how specific C can be. Nothing more. I wanted to understand if C allows to be more specific and free than the other languages (to which I am not aware of because I am new in programming but I have read that C is more close to the assembly and machine languages. That's why I have supposed that declaration of one bit variable could be possible). – sofia.bul Oct 27 '15 at 23:37
  • remember, int is called int because it's fast – user3528438 Oct 27 '15 at 23:47

6 Answers6

3

It is possible only when that variable is enclosed in a struct. Keep in mind that necessary padding will be done in this case.

For example:

struct Bitfield{
    int Bool : 1;
} bit

bit structure will require 4 bytes (assuming int is of width 4 bytes) but only a bit will be used to store the value.

You can declare up to 32 variables each of width 1 for which size of structure bit will be 4 bytes.

Suggested Reading: How is the size of a struct with Bit Fields determined/measured?.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
1

Quick answer: Not in general, no.

Each type has a specific size, each declared object (variable) has a type, and the size occupied by an object is determined by its type. The size of a each type is determined by the rules of the language and, in many cases, by the compiler. For example, an int is at least 16 bits, but may be larger depending on the compiler.

In the case of a bool, assuming your compiler supports it, just declared an object of type _Bool (or bool if you have #include <stdbool.h>. The size will probably be one byte (which is probably 8 bits).

Other than bit fields, it's not possible to have an object whose size is not a whole number of bytes, and a bit field can only exist as a member of a structure (or union).

If you really want an object whose size is a particular number of bytes, then you can define it as an array of unsigned char:

unsigned char obj[42];

If you want an integer that's a certain size, then you can use one of the types declared in <stdint.h>: uint8_t (8 bits), uint16_t (16 bits), etc.

But in most cases, there's no need to do this. Just define your variable using whatever type is appropriate, and let the compiler take care of determining how big they need to be. Unless you're storing or transmitting binary data that needs to be in a specific format, "Size matters not" -- Yoda.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1

Does C language allow us to choose how many bits to reserve for a variable?

The space reserved for a variable depends on its type. You can't directly dictate the bit length of a particular type, but every implementation will offer some types you can use. Some of these types have sizes that are implementation-defined and some of them are fixed.

For example, if we create whole number variable which we will use as bool (1=true, 0=false) we need only one bit. How can I reserve only one bit for that? Is it possible?

It's not possible, because a bit isn't addressable in most architectures and C requires that its types are addressable.

Usually, the extra bits you waste in order to store a boolean value are negligible. If you are working with large amounts of such boolean values and the wasted space isn't negligible, you can always opt to use bitmasks, in order to pack multiple binary values in a larger integer type. Keep in mind that if you choose to do that, you'll be optimizing for space and deoptimizing for speed, because writing or reading isolated bits from a larger type requires multiple operations on most common architectures.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
1

To expand on @haccks answer, if you have a struct like this:

struct foo {
     int flag1:1;
     int flag2:2;
     int somebits:6;
     int somemorebits:12;
     int evenmorebits:10;
};

A foo struct will still take up only 4 bytes.

bruceg
  • 2,433
  • 1
  • 22
  • 29
0

You have some control over this by giving the variable's type. I.e., for integers you can select between char, short, int, long. But the standard is very careful in that it only guarantees that those are in non-decreasing order of size. They might all be exactly the same, as it happened famously in Cray's C (all 32 bits wide).

vonbrand
  • 11,412
  • 8
  • 32
  • 52
0

First of all in any case the minimum addressable unit is byte. So if you are going to use only one bit the compiler will operate with bytes.

You can use bit fields but they may be defined in a structire or union.

Take into account that standard header <stdint.h> contains some definition of more precise integer types.

And for boolean values you can use standard integer type _Bool.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335