布尔数据类型? [英] Boolean data type?

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

问题描述



我来自C ++,而且有一种叫做bool的类型。在C ++中。它的工作原理与任何其他整数类型完全一样,除了在提升时,它会变成一个或零,所以你可以按位运算符,好像

他们是逻辑运算符:


bool a = 5,b = 6;


if(a == b)DoSomething ; / *这将执行* /


在C中常用什么类型来玩布尔值

值?


我正在为嵌入式系统项目编写代码,所以我真的*有* b $ b来观察我正在使用多少内存...所以这是明智的做法是

返回" int"从函数中我将(在C ++中)返回

bool from?例如


int QueryBit(char unsigned const * const pc,unsigned const index)

{

return * pc& (1U<< index);

}


(让我们暂时离开这个问题是否应该

而不是宏)


我想我问两个问题:


a)通常用什么类型booleans?

b)什么类型的常用布尔值当你要轻松

内存消耗?


马丁


I come from C++, and there''s a type called "bool" in C++. It works
exactly like any other integer type except that when promoted, it
either becomes a one or a zero, so you can you bitwise operators as if
they were logical operators:

bool a = 5, b = 6;

if (a == b) DoSomething; /* This will execute */

What type is commonly used in C for playing around with boolean
values?

I''m writing code for an embedded systems project so I''ll *really* have
to watch how much memory I''m using... so would it be wise to be
returning "int" from functions that I would otherwise (in C++) return
a bool from? For example

int QueryBit(char unsigned const *const pc, unsigned const index)
{
return *pc & (1U << index);
}

(Let''s leave alone for the moment the issue of whether this should
instead be a macro)

I suppose I''m asking two questions:

a) What type is commonly used for booleans?
b) What type is commonly used for booleans when you''ve to go easy
on memory consumption?

Martin

推荐答案

Martin Wells写道:
Martin Wells wrote:

>

我来自C ++,而且有一种名为bool的类型。在C ++中。它的工作原理与任何其他整数类型完全一样,除了在提升时,它会变成一个或零,所以你可以按位运算符,好像

他们是逻辑运算符:


bool a = 5,b = 6;


if(a == b)DoSomething ; / *这将执行* /


在C中常用什么类型来玩布尔值

值?
>
I come from C++, and there''s a type called "bool" in C++. It works
exactly like any other integer type except that when promoted, it
either becomes a one or a zero, so you can you bitwise operators as if
they were logical operators:

bool a = 5, b = 6;

if (a == b) DoSomething; /* This will execute */

What type is commonly used in C for playing around with boolean
values?



int a = 5,b = 6;


if(!a ==!b)DoSomething; / *这将执行* /


-

pete

int a = 5, b = 6;

if (!a == !b) DoSomething; /* This will execute */

--
pete


Martin Wells写道:
Martin Wells wrote:

>

我正在编写嵌入式系统项目的代码,所以我真的*真的有*

来观察我正在使用多少内存...因此,返回" int"这将是明智的。从函数中我将(在C ++中)返回

bool from?例如


我想我要问两个问题:


a)布尔通常使用哪种类型?

b)当你的内存消耗很容易时,通常会使用什么类型的布尔值?

>
I''m writing code for an embedded systems project so I''ll *really* have
to watch how much memory I''m using... so would it be wise to be
returning "int" from functions that I would otherwise (in C++) return
a bool from? For example
I suppose I''m asking two questions:

a) What type is commonly used for booleans?
b) What type is commonly used for booleans when you''ve to go easy
on memory consumption?



在这种情况下,如果我没有访问C99编译器或编译器

并将bool作为扩展名,我会用枚举。我使用的大多数嵌入式编译器都有选项,通过使用

最小类型来最小化枚举的大小,而不是保持枚举值的范围。


-

Ian Collins。

In that case, if I didn''t have access to a C99 compiler, or a compiler
with bool as an extension, I''d use an enum. Most embedded compilers I
have used have options to minimise the size of enums by using the
smallest type than holds the range of the enum values.

--
Ian Collins.


9月21日下午4:22,Martin Wells< war .. 。@ eircom.netwrote:
On Sep 21, 4:22 pm, Martin Wells <war...@eircom.netwrote:

我来自C ++,而且有一种叫做bool的类型。在C ++中。它的工作原理与任何其他整数类型完全一样,除了在提升时,它会变成一个或零,所以你可以按位运算符,好像

他们是逻辑运算符:


bool a = 5,b = 6;


if(a == b)DoSomething ; / *这将执行* /


在C中常用什么类型来玩布尔值

值?


我正在为嵌入式系统项目编写代码,所以我真的*有* b $ b来观察我正在使用多少内存...所以这是明智的做法是

返回" int"从函数中我将(在C ++中)返回

bool from?例如


int QueryBit(char unsigned const * const pc,unsigned const index)

{

return * pc& (1U<<指数);


}


(让我们暂时不管这个问题是否应该

而不是宏)


我想我要问两个问题:


a)什么类型通常用于布尔值?

b)当你要轻松消耗内存时,通常使用哪种类型的布尔值?b
$ b
I come from C++, and there''s a type called "bool" in C++. It works
exactly like any other integer type except that when promoted, it
either becomes a one or a zero, so you can you bitwise operators as if
they were logical operators:

bool a = 5, b = 6;

if (a == b) DoSomething; /* This will execute */

What type is commonly used in C for playing around with boolean
values?

I''m writing code for an embedded systems project so I''ll *really* have
to watch how much memory I''m using... so would it be wise to be
returning "int" from functions that I would otherwise (in C++) return
a bool from? For example

int QueryBit(char unsigned const *const pc, unsigned const index)
{
return *pc & (1U << index);

}

(Let''s leave alone for the moment the issue of whether this should
instead be a macro)

I suppose I''m asking two questions:

a) What type is commonly used for booleans?
b) What type is commonly used for booleans when you''ve to go easy
on memory consumption?



如果你有C99 {其中_Bool是一个类型}那么:


ISO / IEC 9899:1999(E) ?ISO / IEC

7.16布尔类型和值< stdbool.h>

1标题< stdbool.hdefines四个宏。

2宏

布尔

扩展为_Bool。

3其余三个宏适用于#if预处理

指令。它们是:

true

扩展为整数常量1,

false

扩展为整数常数0,和

_ _bool_true_false_are_defined

扩展为整数常量1.

4尽管有7.1.3的规定,程序可能undefine和

或许然后重新定义宏bool,true和false.213)

213)参见future library directions (7.26.7)。

252图书馆§7.16


否则只需使用枚举或宏。通常作为枚举:


typedef boolean_type {false = 0,true} bool_t;


通常作为宏:


#ifndef FALSE

#define假0

#endif


#ifndef TRUE

#define TRUE!FALSE

#endif

If you have C99 {where _Bool is a type} then:

ISO/IEC 9899:1999 (E) ?ISO/IEC
7.16 Boolean type and values <stdbool.h>
1 The header <stdbool.hdefines four macros.
2 The macro
bool
expands to _Bool.
3 The remaining three macros are suitable for use in #if preprocessing
directives. They are:
true
which expands to the integer constant 1,
false
which expands to the integer constant 0, and
_ _bool_true_false_are_defined
which expands to the integer constant 1.
4 Notwithstanding the provisions of 7.1.3, a program may undefine and
perhaps then redefine the macros bool, true, and false.213)
213) See "future library directions" (7.26.7).
252 Library §7.16

Else just use enums or macros. Typically as enum:

typedef boolean_type {false=0, true} bool_t;

Typically as macro:

#ifndef FALSE
#define FALSE 0
#endif

#ifndef TRUE
#define TRUE !FALSE
#endif


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

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