检查数字是否使用按位运算符是正还是负 [英] Checking whether a number is positive or negative using bitwise operators

查看:199
本文介绍了检查数字是否使用按位运算符是正还是负的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以检查一个数是奇数/偶数使用位运算符。我可以检查一个数字是否为正/零/负,而无需使用任何条件语句/运营商一样,如果/三元等。

可以在相同的使用按位运算符和C或C ++中的一些伎俩做?

解决方案
  

我可以检查一个数字是否为正/零/负,而无需使用任何条件语句/运营商一样,如果/三元等。

当然:

 布尔is_positive =号> 0;
布尔is_negative =号< 0;
布尔is_zero =号== 0;
 

I can check whether a number is odd/even using bitwise operators. Can I check whether a number is positive/zero/negative without using any conditional statements/operators like if/ternary etc.

Can the same be done using bitwise operators and some trick in C or in C++?

解决方案

Can I check whether a number is positive/zero/negative without using any conditional statements/operators like if/ternary etc.

Of course:

bool is_positive = number > 0;
bool is_negative = number < 0;
bool is_zero = number == 0;

这篇关于检查数字是否使用按位运算符是正还是负的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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