C ++从int转换到bool [英] C++ conversion from int to bool

查看:4368
本文介绍了C ++从int转换到bool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道的编译code进行的布尔对INT 的转换包含一个分支(跳跃)操作。

I want to know if the compiled code of a bool-to-int conversion contains a branch (jump) operation.

例如,由于无效FUNC(布尔B) INT I

时调用的编译code FUNC(我)等同于的编译code FUNC(I 1? 0)

Is the compiled code of calling func(i) equivalent to the compiled code of func(i? 1:0)?

还是有编译器,而无需转移操作执行呢?更加详尽地

Or is there a more elaborate way for the compiler to perform this without the branch operation?

更新:

在换句话说,code确实编译器,以推动1或0进栈跳转到该函数的地址之前产生?

In other words, what code does the compiler generate in order to push 1 or 0 into the stack before jumping to the address of the function?

我假定它确实归结为CPU的结构在手,某些特定的处理器(一定的DSP,例如)可以支持此功能。所以我的问题是指传统的通用CPU(假设这个定义是可以接受的)。

I assume that it really comes down to the architecture of the CPU at hand, and that some specific processors (certain DSPs, for example) may support this. So my question refers to "conventional" general-purpose CPUs (assuming that this definition is acceptable).

在纯软件方面,这个问题也可表述为:有一个整数值转换为1时,它不是0的有效方式,并否则为0,不使用条件语句

In terms of pure software, the question can also be phrased as: is there an efficient way for converting an integer value to 1 when it's not 0, and to 0 otherwise, without using a conditional statement?

感谢

推荐答案

这不是你的(编译器用户)的工作也使内置的类型转换效率。如果编译器是不是哑巴,它会使那种事亲如CPU重新presentation的。

It's not your (compiler user) job too make built-in type conversion efficient. If the compiler is not dumb, it will make that sort of things as close as the CPU representation are.

对于大多数商业CPU,BOOL和INT的是同样的事情,而如果(X){...}
翻译中位安定(或位O型圈,取其速度快:它们通常是直接指令) X 与自身做出后一个条件跳 } 如果零标志设置。 (不,这只是一招,迫使零标志计算,也就是运算单元电子的直接后果)

For the most of the commercial CPU, bool and int are the exact same thing, and if(x) { ... } translate in bit-anding (or bit-oring, whichever is faster: they are normally immediate instructions) x with itself and make a conditional jump after the } if the zero flag is set. (not that this is just a trick to force the zero-flag computation, that is an immediate consequence of the arithmetic unit electronics)

变体是多CPU的电子设备的问题,超过code。所以don'care一下。 如果,则不会被触发布尔,但最后的算术运算结果。

variants are much more a matter of CPU electronics, than code. So don'care about it. ifs are not triggered by a bool, but by the last arithmetic operation result.

无论运算由CPU举行产生的结果ANS设置一些标志是重新present一定的成效属性:如果是零,如果它产生了进位或借,如果它有一个奇数或偶数位置1等Resut和标记是两个寄存器,并且可以加载并存储从/到存储器

Whatever arithmetic operation held by a CPU produces a result ans set some flags that represent certain result attributes: if it is zero, if it produced a carry or borrow, if it has an odd or even number of bit set to 1 etc. Resut and Flags are two registers, and can be loaded and stored from/to memory.

这篇关于C ++从int转换到bool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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