【问题标题】:Saving 16 bit binary data type in C在 C 中保存 16 位二进制数据类型
【发布时间】:2014-09-20 23:25:45
【问题描述】:

如何在 C 中将 16 位二进制存储在数组中?我必须制作什么数据类型的数组?长整数,浮点数,字符? 前任。数据 = {'1001111101110010', '1001101011010101','1000000011010010'}

【问题讨论】:

  • 我认为long double 的使用还不够,所以我会选择struct bin16 { long double bits[16] ; }——如果位变大,我就不必更改表示。 (当然,其他孩子可能会笑,但我妈妈说我会长大的。)
  • @gmch:你在开玩笑吧?
  • 我在想浮动,但也许双倍会更好。 (而且,是的,我也在开玩笑。)

标签: c arrays types binary


【解决方案1】:

我所知道的唯一可以存储类似内容的类型:

{'1001111101110010', '1001101011010101','1000000011010010'}

是一个字符数组:

char *binaryArray[] = {"1001111101110010", "1001101011010101","1000000011010010"}; 

我很确定这不是你想要的。

有几种类型可以用来保存 一个数组位(但它们不是那样呈现的);
unsigned short,
short,
wchar_t,
unsigned __int16
(等人)都有 16 位。 Look here 用于其他可行的数据类型。

选择任何一个可行的,并创建一个 C 位字段 的数组。在 C 中,这可能看起来像:

typedef struct
{
    //type              member name    field width (number of bits in field)
      unsigned short    bits           : 16;
}BIT;    

BIT bit[10];  //array of 10 bit fields, each with capacity for 16 bits    

注意:

赋值如:

bit[0].bits = 40818;  //0x9F72 //1001111101110010   
bit[1].bits = 39637;  //0x9AD5 //1001101011010101   
bit[2].bits = 32978;  //0x08D2 //1000000011010010   

看起来不是二进制的,但它是相等的。

您可以在 this example

中阅读有关位域的更多信息

【讨论】:

    【解决方案2】:

    stdint.h 内是以下typedef

    uint16_t

    这个整数类型的宽度正好是 16 位。您可以像这样使用它来满足您的需求:

    #include <stdint.h>
    
    uint16_t arr[NUM_ELEMENTS] = {...};
    

    【讨论】:

      猜你喜欢
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 2018-06-13
      • 2022-06-26
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      相关资源
      最近更新 更多