定义1位大小的数据类型 [英] define a data type of 1 bit size

查看:70
本文介绍了定义1位大小的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果我太天真了,但这是我的第一篇帖子......

我有一个二进制变量(它可以包含0或1)。

有什么办法可以定义一个只使用1位的数据类型。所以

远我将它定义为char变量。我到处搜索了但是

我好像找不到任何解释如何定义这种类型的地方

的数据类型。我发现最接近的是

结构中的位字段,我想像位字段但没有

结构。

类似

unsigned MyVariable:1;

谢谢,

天使

Sorry if I am too naive, but this is my first post...
I have a binary variable (it can contain either a 0 or a 1).
Is there any way I can define a data type that uses only 1 bit. So
far I have defined it as a char variable. I''ve searched everywhere but
I don''t seem to find any place that explains how to define this type
of data type. The closest thing I''ve found are bit fields in
structures, I would like something like bit fields but without the
structure.
something like
unsigned MyVariable :1;
Thanks,
Angel

推荐答案

" Angel Lopez" <一个******* @ hotmail.com>在消息中写道

新闻:9d ************************** @ posting.google.c om ...
"Angel Lopez" <an*******@hotmail.com> wrote in message
news:9d**************************@posting.google.c om...
抱歉,如果我太天真,但这是我的第一篇文章...
我有一个二进制变量(它可以包含0或1)。
是有什么办法可以定义一个只使用1位的数据类型。所以
我把它定义为char变量。我到处搜索但是我似乎找不到任何解释如何定义这种数据类型的地方。我发现最接近的是
结构中的位字段,我想像位字段但没有
结构。
Sorry if I am too naive, but this is my first post...
I have a binary variable (it can contain either a 0 or a 1).
Is there any way I can define a data type that uses only 1 bit. So
far I have defined it as a char variable. I''ve searched everywhere but
I don''t seem to find any place that explains how to define this type
of data type. The closest thing I''ve found are bit fields in
structures, I would like something like bit fields but without the
structure.




如果你没有太多它们,使用内置类型如char或

甚至int也没关系 - 只需使用0表示0,非0表示1。


如果你想要一个数组中的很多(和很多)它们,你可以节省内存和

通过使用无符号类型(如unsigned
int并编写简单的函数或宏来通过指定

位索引来访问它们。最佳类型将取决于平台,但是很容易编写代码,因此当您发现

性能时,您可以将类型更改为实验一个问题。


一些微控制器的编译器(例如8051-alikes)扩展了

这种允许你定义位变量的语言。位变量

通常在这些内存可能非常有限的嵌入式环境中更有用。


Alex



If you don''t have too many of them, using a built-in type such as char or
even int is fine - just use zero for 0 and non-zero for 1.

If you want lots (and lots) of them in an array, you can save memory and
perhaps get better performance by using an unsigned type such as unsigned
int and writing simple functions or macros to access them by specifying a
bit index. The optimal type will depend on the platform, but it''s easy to
write code so you can change the type to experiment if and when you find
performance to be a problem.

Compilers for some microcontrollers (such as 8051-alikes) have extensions to
the language that allow you to define bit variables. Bit variables are
typically more useful in these embedded environments where memory may be
very limited.

Alex


Angel Lopez写道:
Angel Lopez wrote:
抱歉,如果我太天真,但这是我的第一篇帖子...
我有一个二进制变量(它可以包含0或1)。
有没有什么办法可以定义一个只使用1位的数据类型。所以
我把它定义为char变量。我到处搜索但是我似乎找不到任何解释如何定义这种数据类型的地方。我发现最接近的是
结构中的位字段,我想像位字段但没有
结构。

unsigned MyVariable:1; <谢谢,
天使
Sorry if I am too naive, but this is my first post...
I have a binary variable (it can contain either a 0 or a 1).
Is there any way I can define a data type that uses only 1 bit. So
far I have defined it as a char variable. I''ve searched everywhere but
I don''t seem to find any place that explains how to define this type
of data type. The closest thing I''ve found are bit fields in
structures, I would like something like bit fields but without the
structure.
something like
unsigned MyVariable :1;
Thanks,
Angel



在C99你可以写:

#include< stdbool.h>


bool myvar = 1;


这将采用char(8位)。你不能解决这个问题,所以这将是所有编译器中相同的
。优点是,如果你写了

myvar = 78;

printf("%d \ nn," myvar);

那个将打印1而不是78.


jacob


In C99 you can write:
#include <stdbool.h>

bool myvar = 1;

This will take a char (8 bits). You can''t address bits so this will be
the same in all compilers. The advantage is that if you write
myvar=78;
printf("%d\n",myvar);
that will print 1 and not 78.

jacob


MVC ++ 6允许布尔类型,它几乎无处可移植并占用反正
字节。虽然我从未关注ANSI,但我认为这笔交易是

,字节总是有8位,所有数据类型都是

字节的倍数。你当然可以编写一个程序来将8个1或0个b / b
压缩成一个字节,但我认为调用什么产生适当的数据是一个很好的结果

类型。 MPJ

" Angel Lopez" <一个******* @ hotmail.com>在消息中写道

新闻:9d ************************** @ posting.google.c om ...
MVC++6 allows a boolean type which will port almost nowhere and takes up a
byte anyways. Although I''ve never laid eyes on ANSI, I thought the deal was
that bytes always have eight bits and all data types are a multiple of
bytes. You could certainly write a program to squeeze eight ones or zeros
into a byte, but I think it''s a stretch to call what results a proper data
type. MPJ
"Angel Lopez" <an*******@hotmail.com> wrote in message
news:9d**************************@posting.google.c om...
抱歉,如果我太天真,但这是我的第一篇文章...
我有一个二进制变量(它可以包含0或1)。
是有什么办法可以定义一个只使用1位的数据类型。所以
我把它定义为char变量。我到处搜索但是我似乎找不到任何解释如何定义这种数据类型的地方。我发现最接近的是
结构中的位字段,我想像位字段但没有
结构。

unsigned MyVariable:1; <谢谢,
天使
Sorry if I am too naive, but this is my first post...
I have a binary variable (it can contain either a 0 or a 1).
Is there any way I can define a data type that uses only 1 bit. So
far I have defined it as a char variable. I''ve searched everywhere but
I don''t seem to find any place that explains how to define this type
of data type. The closest thing I''ve found are bit fields in
structures, I would like something like bit fields but without the
structure.
something like
unsigned MyVariable :1;
Thanks,
Angel



这篇关于定义1位大小的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆