让x = (0&0xFFFFFFFF) + ~0 +1 ,x 的值是多少?

发布于 2025-01-17 10:20:46 字数 441 浏览 7 评论 0原文

我正在研究AC位作业,因为它要求我在不使用'!'的情况下实施逻辑否定的问题之一;这就是我想到的:

`

(0 & 0xFFFFFFFF) // S1: 0s & ones should return 0s right?

+ ~0 +1  // S2: then to the value above I add 1 and the not value of 0 (which in my understanding is 0xFFFFFFFF)

`

现在以人类语言,S1结果:0x0000,s2:0x0000-0 + 1 最终应该返回1。我得到0。我在哪里出错?

预先感谢:)

编辑: 您认为如果修改这种方法有希望吗?并计算一个事实,即我可以通过一个非零的数字,结果给我0。 !(n)= 0,除非n == 0,否则它应该返回1。

I'm working on a c bit homework, for one of the questions it is asking me to implement logical negation without using '!'; this is what I came up with:

`

(0 & 0xFFFFFFFF) // S1: 0s & ones should return 0s right?

+ ~0 +1  // S2: then to the value above I add 1 and the not value of 0 (which in my understanding is 0xFFFFFFFF)

`

Now in human language, S1 result: 0x0000 , S2: 0x0000 - 0 + 1
which should end up returning 1. I instead get 0. Where am I going wrong here??

Thanks in advance :)

edit:
do you think there's hope for this approach if modified? and counting the fact that I can pass a non zero number that should give me a 0 as a result. !(n) = 0 unless n ==0 then it should return 1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三人与歌 2025-01-24 10:20:46

(0& 0xffffffff)当然会形成无符号0无符号长度0作为0xfffffffffff是十六进制的常数, first fits into one of those 2 types. @Eric

0是A 签名0。〜0翻转所有位 - 结果保留签名。有了非常常见的2补充编码,这是A 签名 -1。

然后, 0与〜0变为无符号0xffffffff0xffff as -1首先转换为-1一个unsigned等。取决于int/intemed的位宽度。

将1添加1,值为0。结果为 unsigned


我在哪里出错?

添加〜0不像减去0


在不使用'!';

的情况下实现逻辑否定

这需要移动或IFS并经常实现代码或简单地:

y = x == 0;

soapbox soapbox :IMO,这是一个学习者的分配不佳,因为它鼓励实施依赖于实施代码。 C有一个原因。

(0 & 0xFFFFFFFF) certainly forms an unsigned 0 or unsigned long 0 as 0xFFFFFFFF is a hexadecimal constant that first fits into one of those 2 types. @Eric

0 is a signed 0. ~0 flips all the bits - the result remains signed. With the very common 2's complement encoding, this is a signed -1.

Then the addition of an unsigned 0 with ~0 becomes unsigned 0xFFFFFFFF or 0xFFFF as -1 is first converted to an unsigned, etc. depending on the bit width of int/unsigned.

Adding 1 to that, the value is 0. The result is unsigned.


Where am I going wrong here?

Adding ~0 is not like subtracting 0.


implement logical negation without using '!';

This requires shifts or ifs and often implementation depended code or simply:

y = x == 0;

Soapbox: IMO, it is a poor assignment for a learner as it encourages implementation dependent code. C has !` for a reason.

维持三分热 2025-01-24 10:20:46

现在使用人类语言,S1结果:0x0000,S2:0x0000-0 + 1

〜0不是-0,它是-1。如有必要,请阅读两者的补充编码。

所以你的表达方式是:

(0 & 0xFFFFFFFF) + ~0 +1 =
(0 & whatever) + ~0 + 1 =
0 + ~0 + 1 =
~0 + 1 =
-1 + 1 =
0

Now in human language, S1 result: 0x0000 , S2: 0x0000 - 0 + 1

~0 is not -0, it's -1. If necessary, read up on two's complement encoding.

So your expression is:

(0 & 0xFFFFFFFF) + ~0 +1 =
(0 & whatever) + ~0 + 1 =
0 + ~0 + 1 =
~0 + 1 =
-1 + 1 =
0
梦醒灬来后我 2025-01-24 10:20:46

每个操作数具有类型int的表达式

~0 +1

等同于(如果使用内部表示)

0xFFFFFFFF + 1

,那又等同

-1 + 1

于编译器使用2个整数的补充表示,以产生> code> 0

因此,s1s2均等于0,因此结果也等于0

This expression in which each operand has the type int

~0 +1

is equivalent to (if to use the internal representation)

0xFFFFFFFF + 1

that in turn is equal to

-1 + 1

provided that the compiler uses the 2's complement representation of integers that yields 0.

So S1 and S2 are both equal to 0 and hence the result is also equal to 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文