【问题标题】:How to use bitwise operators?如何使用位运算符?
【发布时间】:2014-02-10 17:20:13
【问题描述】:

我正在尝试编写一个可以在不使用 - 运算符的情况下对整数求反的函数。

函数是 int negate(int n),谁能告诉我如何在 C 编程中做到这一点?

【问题讨论】:

标签: c function operators bit-manipulation


【解决方案1】:

我说 C#,但在 C 中应该是一样的。试试这个。

int negate(int n){
    return ~n + 1; // or you could use ~(n - 1)
}

保重。

【讨论】:

    猜你喜欢
    • 2011-05-30
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多