执行逻辑“不”!仅使用按位运算

发布于 2024-09-29 06:31:52 字数 363 浏览 1 评论 0原文

可能的重复:
使用以下命令检查数字是否非零C 中的按位运算符。

大家好,

我正在做一个项目,我需要一些关于函数的帮助。我们需要编写一个函数来执行逻辑非,!,仅使用以下按位运算符:

~ & ^ | + << >>

我什至不知道从哪里开始。

Possible Duplicate:
Check if a number is non zero using bitwise operators in C.

Hello everyone,

I am working on a project and I need a little help with a function. We need to write a function that performs the logical not, !, using only the following bitwise operators:

~ & ^ | + << >>

I'm not even sure where to begin.

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

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

发布评论

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

评论(3

空‖城人不在 2024-10-06 06:31:52

如果您可以假设 true = 1false = 0,那么这可能会起作用:

bool
not(bool x) {
    bool not_x = x ^ true;
    return not_x;
}

If you can assume that true = 1 and false = 0, then this might do the trick:

bool
not(bool x) {
    bool not_x = x ^ true;
    return not_x;
}
嘿看小鸭子会跑 2024-10-06 06:31:52

如果值不为零,逻辑非返回 0,否则返回 1。假设 32 位 int:

int not_func(int v) {
    /* compress value to single bit */
    int p = (v >> 16) | v;
    p = (p >> 8) | p;
    p = (p >> 4) | p;
    p = (p >> 2) | p;
    p = (p >> 1) | p;

    p ^= 1;
    return (p & 1);
}

Logical not returns 0, if value is not zero, 1 otherwise. Assuming 32-bit int:

int not_func(int v) {
    /* compress value to single bit */
    int p = (v >> 16) | v;
    p = (p >> 8) | p;
    p = (p >> 4) | p;
    p = (p >> 2) | p;
    p = (p >> 1) | p;

    p ^= 1;
    return (p & 1);
}
故事与诗 2024-10-06 06:31:52

我认为首先你需要澄清这个问题。听起来您想要一个函数,如果单词中的任何位为“1”,则返回 0;如果所有位都为零,则返回 0 以外的值。假设有一个 32 位单词“a”,你可以这样做:

na1 = ~a;
shifted_na1 = na1 >> 1;
na2 = shifted_na1 & na1; /* any sequence of zeros is now 2 bits long */
shifted_na2 = na2 >> 2;
na3 = shifted_na2 & na2; /* any sequence of zeros is now 4 bits long */
shifted_na3 = na3 >> 4;
na4 = shifted_na3 & na3; /* any sequence of zeros is now 8 bits long */
shifted_na4 = na4 >> 8;
na5 = shifted_na4 & na4; /* any sequence of zeros is now 16 bits long */
shifted_na5 = na5 >> 16;
final = shifted_na5 & na5; /* any sequence of zeros is now 32 bits long */
really_final = final & 1;

I think to start you want you'll want to clarify the question. It sounds like you're wanting at a function that will return 0 if any of the bits in a word are "1", and something other than 0 if all the bits are zero. Assuming a 32bit word "a" you could do something like:

na1 = ~a;
shifted_na1 = na1 >> 1;
na2 = shifted_na1 & na1; /* any sequence of zeros is now 2 bits long */
shifted_na2 = na2 >> 2;
na3 = shifted_na2 & na2; /* any sequence of zeros is now 4 bits long */
shifted_na3 = na3 >> 4;
na4 = shifted_na3 & na3; /* any sequence of zeros is now 8 bits long */
shifted_na4 = na4 >> 8;
na5 = shifted_na4 & na4; /* any sequence of zeros is now 16 bits long */
shifted_na5 = na5 >> 16;
final = shifted_na5 & na5; /* any sequence of zeros is now 32 bits long */
really_final = final & 1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文