Esentially I want to create a data type uint1_t
. Is that even possible?
I know the size of the bool data type is one byte. But boolean values only need
Short answer is "no"; except for bit-fields, all types must map to a whole number of bytes (and multiple bit-fields will occupy the same byte if they can all fit).
From the horse's mouth:
6.2.6 Representations of types
6.2.6.1 General
1 The representations of all types are unspecified except as stated in this subclause.
2 Except for bit-fields, objects are composed of contiguous sequences of one or more bytes, the number, order, and encoding of which are either explicitly specified or implementation-defined.
3 Values stored in unsigned bit-fields and objects of type unsigned char shall be represented using a pure binary notation.49)
4 Values stored in non-bit-field objects of any other object type consist of n ×CHAR_BIT
bits, where n is the size of an object of that type, in bytes. The value may be copied into an object of typeunsigned char [n]
(e.g., bymemcpy
); the resulting set of bytes is called the object representation of the value. Values stored in bit-fields consist of m bits, where m is the size specified for the bit-field. The object representation is the set of m bits the bit-field comprises in the addressable storage unit holding it. Two values (other than NaNs) with the same object representation compare equal, but values that compare equal may have different object representations
49) A positional representation for integers that uses the binary digits 0 and 1, in which the values represented by successive bits are additive, begin with 1, and are multiplied by successive integral powers of 2, except perhaps the bit with the highest position. (Adapted from the American National Dictionary for Information Processing Systems.) A byte containsCHAR_BIT
bits, and the values of typeunsigned char
range from 0 to 2CHAR_BIT − 1
.
The closest thing you can come to something like that is by using bit fields. They are set up within a struct
and each field of the struct
determines its width.
Example:
struct foo
{
unsigned int bla :1; /* this uses only 1 bit */
}
This case still 'wastes' the other bits of the int
but if you had other fields you could effectively use each bit of the int
to represent a Boolean value
http://en.wikipedia.org/wiki/Bit_field
The smallest object you can create has a sizeof == 1
. That object will be CHAR_BIT
bits in size, which on almost every platform you'll ever see, will be 8.
So the smallest object you can create is a int8_t
aka char
.
You can do things with bitfields to encode many 1 bit numbers into a larger object, but that's not exactly a solution to your problem.
Contrary to what some people believe, there is a data type of one bit in C99: it's called _Bool
. You can also declare bitfields of size 1. The fact that individual bits are not addressable in C does not mean that one-bit data types cannot exist. That argument is basically comparing apples to oranges.
There isn't, however, a type of which the storage size (sizeof
) is less than one byte.
It is not really possible to create a type that occupies one bit. The smallest addressable unit in C is the char
(which is by definition one byte and usually, but not necessarily, 8 bits long; it might be longer but isn't allowed to be shorter than 8 bits in Standard C).
You can approach it with :
typedef _Bool uint1_t;
or:
#include <stdbool.h>
typedef bool uint1_t;
but it will occupy (at least) one byte, even though a Boolean variable only stores the values 0 or 1, false
or true
.
You could, in principle, use a bit-field:
typedef struct
{
unsigned int x : 1;
} uint1_t;
but that will also occupy at least one byte (and possibly as many bytes as an unsigned int
; that's usually 4 bytes) and you'll need to use .x
to access the value. The use of bit-fields is problematic (most aspects of them are implementation defined, such as how much space the storage unit that holds it will occupy) — don't use a bit-field.
Including amendments suggested by Drew McGowen, Drax and Fiddling Bits.
No. 8 bits is the minimum size for a type. You can use a bit field to combine multiple "small" elements together if you really needed sub-byte storage.
However this is rarely a big deal. Machines have lots of memory and it is rarely necessary to worry about this kind of memory waste.