布尔逻辑混乱 [英] Boolean logic confusion

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

问题描述

我不完全了解按位运算和逻辑运算之间的区别.请参见下面的按位操作:

I do not fully understand the difference between a bitwise operation and a logical operation. Please see the bitwise operation below:

x = 7和15

x=7 and 15

我知道在这种情况下,逐个检查每个位后,x等于7.

I understand that x will equal 7 in this case after inspecting each bit individually.

布尔逻辑在较低级别如何工作.我认为布尔值是32位数据类型(我可能错了).布尔文字(TRUE和FALSE)是否被视为单个位?

How does boolean logic work at a lower level. I believe that a Boolean is a 32 Bit data type (I could be wrong). Are boolean literals (TRUE and FALSE) treated as single bits?

推荐答案

这通常是这些东西的工作方式.

This is generally how these things work.

按位运算对表示其操作数的位执行布尔运算.逻辑运算是对布尔集执行的布尔运算,通常为TRUE和FALSE或1和0.布尔集可以是任意不同的项目组,其中集合成员的总数为2.

Bitwise operations perform Boolean operations on the bits representing their operands. Logical operations are Boolean operations performed on Boolean sets, usually TRUE and FALSE or 1 and 0. A Boolean set could be any group of distinct items where the total count of set members is 2.

在对操作数7和15执行按位与运算的情况下:如果您使用的系统将它们表示为没有奇怪偏移的正二进制数,则二进制数7变为0111,二进制数15变为1111.二进制7上的前导0并不是必需的,因为它与在任何十进制数字前加一个零是相同的:10 = 010 = 000000000000010,尽管更容易说明前导零的运算.

In the case of performing bitwise AND on the operands 7 and 15: if the system you're using represents them as straight binary numbers with no weird offsets then 7 becomes 0111 in binary, 15 becomes 1111 in binary. The leading 0 on binary 7 is not necessary as it is the same as prepending a zero to any decimal number: 10 = 010 = 000000000000010, it's easier to illustrate the operation with the leading zero though.

0111   7 in binary
1111  15 in binary
&&&&  bitwise AND
0111  results in 7

如果truefalse表示为二进制1和0,则逻辑" and和按位" and操作之间没有区别.

if true and false are represented as binary 1 and 0 then there is no difference between the "logical" and and the "bitwise" and operations.

1   true as binary 1
0   false as binary 0
&   either logical or bitwise AND
0   results in binary 0

对其他数字(例如1和2)按位and尝试

try bitwise and on some other numbers like say 1 and 2

01 1 in binary
10 2 in binary
&& bitwise and
00 results in binary 0

假设除0以外的任何数字都将转换为true,对数字1和2进行逻辑" and会产生不同的结果

Assuming that any number except 0 will be converted to true, performing a "logical" and on the numbers 1 and 2 would yield a different result

1 the number 1 converted to true and represented as boolean 1
1 the number 2 converted to true and represented as boolean 1
& logical and
1 results in true, here represented as binary 1

当执行涉及浮点数,负数或除正整数以外的任何其他运算时,最终可能会得到截然不同的结果.这是由于不同的环境使用不同的方法在内存中存储数字所致.当然,它们都是二进制的,但是数字行并不总是以二进制零为中心,也不总是每个位都代表数字的一部分.一些位可能代表数字或指数的符号.如果您只想将小数转换为按位运算,则必须深入研究实现细节.如果您有一种将数字转换为二进制的方法,则处理起来会更容易,因为这样您就可以准确了解位及其顺序,并且可以测试期望值是否与结果相符.

When performing bitwise operations involving floats, negative numbers, or basically anything other than positive whole numbers, you may end up with radically different results. This is due to different environments using different methods for storing numbers in memory. For sure, it's all binary but, the number line isn't always centered on binary zero and it's not always the case that every bit represents part of the number; some of the bits could represent the sign of the number or the exponent. You'll have to dig down into the implementation details if you want to just toss decimal numbers into bitwise operations. It's easier to deal with if you have a method of converting the numbers to binary because then you know the bits and their order exactly and, you'll be able to test whether your expectations match your results.

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

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