是否可以在C中创建长度为1位的数据类型

时间:2021-09-09 16:12:23

Esentially I want to create a data type uint1_t. Is that even possible?

基本上我想创建一个数据类型uint1_t。这有可能吗?

I know the size of the bool data type is one byte. But boolean values only need one bit. So does C essentially only use one bit for bool? If yes, then what does it do with the other seven. Using eight bits where one is sufficient seems such a waste of space.

我知道bool数据类型的大小是一个字节。但是布尔值只需要一位。那么C基本上只使用一位用于布尔?如果是,那么它与其他七个有什么关系呢。在一个足够的情况下使用八位似乎是浪费空间。

8 个解决方案

#1


14  

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).

创建一个占据一位的类型是不可能的。 C中最小的可寻址单元是char(根据定义,它是一个字节,通常但不一定是8位长;它可能更长,但不允许短于标准C中的8位)。

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 also use a bit-field:

但它会占用(至少)一个字节,即使布尔变量只存储值0或1,false或true。您还可以使用位字段:

typedef struct
{
    unsigned int x : 1;
} uint1_t;

but that will also occupy (at least) one byte and you'll need to use .x to access the value.

但这也会占用(至少)一个字节,你需要使用.x来访问该值。

#2


6  

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.

与某些人认为的相反,C99中有一位数据类型:它叫做_Bool。您还可以声明大小为1的位域。单个位在C中不可寻址的事实并不意味着不能存在一位数据类型。这个论点基本上是将苹果与橙子进行比较。

There isn't, however, a type of which the storage size (sizeof) is less than one byte.

但是,存储大小(sizeof)小于一个字节的类型不存在。

#3


4  

No this is not possible, uint8_t is the smallest data type. Within struct you could use bit fields, besides that not possible to have a data type of just 1 bit.

不,这是不可能的,uint8_t是最小的数据类型。在struct中你可以使用位字段,除了不可能只有1位的数据类型。

#4


3  

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.

你可以创建的最小对象的sizeof == 1.该对象的大小为CHAR_BIT位,几乎在你所见过的每个平台上都是8。

So the smallest object you can create is a int8_t aka char.

因此,您可以创建的最小对象是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.

您可以使用位域来将许多1位数编码成更大的对象,但这并不是解决您问题的方法。

#5


2  

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

这种情况仍然“浪费”int的其他位,但如果你有其他字段,你可以有效地使用int的每个位来表示一个布尔值

http://en.wikipedia.org/wiki/Bit_field

http://en.wikipedia.org/wiki/Bit_field

#6


1  

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 type unsigned char [n] (e.g., by memcpy); 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 contains CHAR_BIT bits, and the values of type unsigned char range from 0 to 2CHAR_BIT − 1.

#7


0  

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.

8位是一种类型的最小尺寸。如果您确实需要子字节存储,则可以使用位字段将多个“小”元素组合在一起。

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.

然而,这很少是一个大问题。机器有很多内存,很少需要担心这种内存浪费。

#8


0  

Yes you can create a one bit variable suppose int a:1; You can just assign value to it but cannot scanf it.

是的你可以创建一个位变量假设int a:1;您可以为其分配值但无法扫描它。

#1


14  

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).

创建一个占据一位的类型是不可能的。 C中最小的可寻址单元是char(根据定义,它是一个字节,通常但不一定是8位长;它可能更长,但不允许短于标准C中的8位)。

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 also use a bit-field:

但它会占用(至少)一个字节,即使布尔变量只存储值0或1,false或true。您还可以使用位字段:

typedef struct
{
    unsigned int x : 1;
} uint1_t;

but that will also occupy (at least) one byte and you'll need to use .x to access the value.

但这也会占用(至少)一个字节,你需要使用.x来访问该值。

#2


6  

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.

与某些人认为的相反,C99中有一位数据类型:它叫做_Bool。您还可以声明大小为1的位域。单个位在C中不可寻址的事实并不意味着不能存在一位数据类型。这个论点基本上是将苹果与橙子进行比较。

There isn't, however, a type of which the storage size (sizeof) is less than one byte.

但是,存储大小(sizeof)小于一个字节的类型不存在。

#3


4  

No this is not possible, uint8_t is the smallest data type. Within struct you could use bit fields, besides that not possible to have a data type of just 1 bit.

不,这是不可能的,uint8_t是最小的数据类型。在struct中你可以使用位字段,除了不可能只有1位的数据类型。

#4


3  

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.

你可以创建的最小对象的sizeof == 1.该对象的大小为CHAR_BIT位,几乎在你所见过的每个平台上都是8。

So the smallest object you can create is a int8_t aka char.

因此,您可以创建的最小对象是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.

您可以使用位域来将许多1位数编码成更大的对象,但这并不是解决您问题的方法。

#5


2  

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

这种情况仍然“浪费”int的其他位,但如果你有其他字段,你可以有效地使用int的每个位来表示一个布尔值

http://en.wikipedia.org/wiki/Bit_field

http://en.wikipedia.org/wiki/Bit_field

#6


1  

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 type unsigned char [n] (e.g., by memcpy); 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 contains CHAR_BIT bits, and the values of type unsigned char range from 0 to 2CHAR_BIT − 1.

#7


0  

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.

8位是一种类型的最小尺寸。如果您确实需要子字节存储,则可以使用位字段将多个“小”元素组合在一起。

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.

然而,这很少是一个大问题。机器有很多内存,很少需要担心这种内存浪费。

#8


0  

Yes you can create a one bit variable suppose int a:1; You can just assign value to it but cannot scanf it.

是的你可以创建一个位变量假设int a:1;您可以为其分配值但无法扫描它。