查看: 1595| 回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 5/14-5/20 CareerCup 5.4

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
Explain what the following code does: ((n & (n - 1)) == 0)

上一篇:5/14-5/20 CareerCup 5.3
下一篇:5/14-5/20 CareerCup 5.5
🔗
writecoffee1 2012-6-9 20:14:08 | 只看该作者
全局:
https://github.com/writecoffee/c ... blob/master/5.4.cpp


////////////////////////////////////////////////////////////////////////////////////
// GIST
//         judge whether n has only one bit flipped on or not
//
// WHY IS THAT
// 1.        n-1 flips the least significant 1 bit (x) and all zero bit lower than that,
//        so excluding the higher bits (for those between bit (x+1) and 31), n&(n-1)
//        generates 0.
// 2.        after that, we can put sight on bit between (x+1) and 31, for which can
//        influence the final result
//       
// OBSERVATION
//         by observation, this expression can only apply to positive interger
//        and the smallest negative number (-2^31), which most significant bit
//        is set to 1; except that, all negtive number has at least 2 bits on
////////////////////////////////////////////////////////////////////////////////////
回复

使用道具 举报

🔗
paradox 2012-6-13 10:21:59 | 只看该作者
全局:
writecoffee1 发表于 2012-6-9 20:14
https://github.com/writecoffee/career_cup_iteration/blob/master/5.4.cpp

同上,经典题 了
回复

使用道具 举报

🔗
lml 2012-8-18 07:51:30 | 只看该作者
全局:
判断n是不是只有一个bit为1,连续的用这种方法可以count n中1的个数
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表