【发布时间】:2015-08-04 09:00:47
【问题描述】:
n & (n>>1)
我在哪里可以使用上面的表达式?我在做this 问题,我看到this 解决了使用表达式的问题。
问题-
You are given an integer n find its next greater or equal number whose
binary representation must not contain consecutive ones.
代码-
main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
while((n&(n>>1)))
{
n++;
}
printf("%d\n",n);
}
}
【问题讨论】:
-
表达式逻辑上表示
AND和n和n,其位右移一位。因此,无论您在哪里这样做,您的表达都会派上用场。如果您发布使用此表达式的代码,则可能会说更多。
标签: c++ bitwise-operators