就十六进制结果而言,按位与逻辑“与/或"运算 [英] bitwise and logical AND/OR in terms of hex result

查看:1288
本文介绍了就十六进制结果而言,按位与逻辑“与/或"运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以如果我有x = 0x01y = 0xff并且我做x & y,我会得到0x01.如果我执行x && y,我仍然会获得0x01,但是计算机只是说出它的真实性(如果它不是0x00的话)?我的问题是,无论按位运算或逻辑与/或运算,运算后的位是否相同,但是计算机对最终结果的解释不同?换句话说,是&的结果的十六进制值.和&& (同样|和||)是否相同?

在这里谈论C

解决方案

答案取决于所使用的确切语言.

在某些弱类型语言(例如Perl,JavaScript)中,如果ab都是真实的",则a && b将计算为b的实际值,否则为a.

在C和C ++中,&&运算符将仅返回0或1,即与true等效的int或false.

在Java和C#中,向&&提供int甚至是非法的-操作数必须已经是布尔值.

对于ab的所有合法值,我不知道a && b == a & b是哪一种语言.

so if I have x = 0x01 and y = 0xff and I do x & y, I would get 0x01. If I do x && y do I still get 0x01, but the computer just says its true if its anything than 0x00? My question is are the bits the same after the operation regardless of bit-wise or logical AND/OR, but the computer just interprets the final result differently? In other words are the hex values of the result of & and && (likewise | and ||) operations the same?

edit: talking about C here

解决方案

The answer depends on the precise language in use.

In some weakly-typed languages (e.g. Perl, JavaScript) a && b will evaluate to the actual value of b if both a and b are "truthy", or a otherwise.

In C and C++ the && operator will just return 0 or 1, i.e. the int equivalent of true or false.

In Java and C# it's not even legal to supply an int to && - the operands must already be booleans.

I don't know of any language off the top of my head where a && b == a & b for all legal values of a and b.

这篇关于就十六进制结果而言,按位与逻辑“与/或"运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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