差b/w 1 == n& 1和n& 1 == 1,n是一个无符号整数

编程入门 行业动态 更新时间:2024-10-16 02:23:50
本文介绍了差b/w 1 == n& 1和n& 1 == 1,n是一个无符号整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是来自leetcode的程序问题.它提供了一个未签名的int n,请您返回位"1"的数字;

this is a program problem from leetcode.it gives an unsigned int n,ask you to return the numbers of bit '1';

int hammingWeight(uint32_t n) { int num=0; for(;n!=0;n=n>>1) { if(n&1==1) { num++; } } return num; }

这很好用,但是

int hammingWeight(uint32_t n) { int num=0; for(;n!=0;n=n>>1) { if(1==n&1) { num++; } } return num; }

这有时不能工作!我猜想当用uint32_t计算1时出了点问题,但是我无法清楚地理解这一点.

this one cant work sometimes! i guess something is wrong when 1 calculate with an uint32_t,but i cant understand this clearly.

推荐答案

==的优先级高于&.因此,

== has higher precedence than &. Thus,

  • n&1==1是n & (1==1),而
  • 1==n&1是(1==n) & 1.
  • n&1==1 is n & (1==1), while
  • 1==n&1 is (1==n) & 1.

更多推荐

差b/w 1 == n& 1和n& 1 == 1,n是一个无符号整数

本文发布于:2023-11-29 11:57:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646333.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是一个   整数   符号   amp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!