按位经营者不 [英] bitwise not operator

查看:155
本文介绍了按位经营者不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么按位运算(〜0); 打印-1?在二进制,而不是0应为1。为什么?

Why bitwise operation (~0); prints -1 ? In binary , not 0 should be 1 . why ?

推荐答案

您实际上是相当密切的。

You are actually quite close.

在二进制,而不是0应该是1

In binary , not 0 should be 1

是的,这绝对是正确的,当我们谈论一位。

Yes, this is absolutely correct when we're talking about one bit.

然而, INT ,其值为0实际上是所有零的32位! 反转所有32个零到32人。

HOWEVER, an int whose value is 0 is actually 32 bits of all zeroes! ~ inverts all 32 zeroes to 32 ones.

System.out.println(Integer.toBinaryString(~0));
// prints "11111111111111111111111111111111"

这是两个补重presentation 1

This is the two's complement representation of -1.

同样的:

System.out.println(Integer.toBinaryString(~1));
// prints "11111111111111111111111111111110"

也就是说,对于一个32位无符号 INT 在补重presentation,〜1 == -2

That is, for a 32-bit unsigned int in two's complement representation, ~1 == -2.

延伸阅读:


  • 两补

    • 这是Java中使用(其中包括)重新系统present中位签订数值


    • 指出,在所有情况下,〜X 等于( - X)-1
    • "note that, in all cases, ~x equals (-x)-1"

    这篇关于按位经营者不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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