【问题标题】:What is "~" operator in JavaJava中的“~”运算符是什么
【发布时间】:2015-06-14 04:44:45
【问题描述】:

有人可以帮忙解释一下“~”在 Java 中的作用吗?我搜索但找不到答案,因为我不知道 ~ 叫什么。提前致谢。下面是一个名为 Snow 的开源隐秘程序的示例。

private int     tabpos (int n) {
    return ((n + 8) & ~7);
}

【问题讨论】:

  • 我相信按位否定。所以在这种情况下,~7 中除了最小的 3 位之外的所有位都为零。
  • “~”字符被称为“波浪号”。

标签: java operator-keyword symbols


【解决方案1】:

来自 Oracle 文档:

The unary bitwise complement operator "~" inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a byte contains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111".

参考:Bitwise and Bit Shift Operators

【讨论】:

    猜你喜欢
    • 2012-11-21
    • 2017-10-13
    • 2020-02-07
    • 2012-06-15
    • 2020-05-22
    • 1970-01-01
    • 2011-04-02
    • 2020-05-25
    • 2017-05-10
    相关资源
    最近更新 更多