博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Position of rightmost set bit
阅读量:4151 次
发布时间:2019-05-25

本文共 553 字,大约阅读时间需要 1 分钟。

reference: 

Problem Definition:

Write a one line C function to return position of first 1 from right to left, in binary representation of an Integer.

Solution:

Example 18(010010)
1. Take two's complement(i.e. -n) of the given no as all bits are revertedexcept the first '1' from right to left (10111)2  Do an bit-wise & with original no, this will return no with therequired one only (00010)3  Take the log2 of the no, you will get position -1 (1)4  Add 1 (2)

Code:

unsigned int getFirstSetBitPos(int n){   return log2(n&-n)+1;//or log2(n & (n^(n-1)))+1}

转载地址:http://uexti.baihongyu.com/

你可能感兴趣的文章
年薪50万+的90后程序员都经历了什么?
查看>>
2019年哪些外快收入可达到2万以上?
查看>>
【JavaScript 教程】标准库—Date 对象
查看>>
前阿里手淘前端负责人@winter:前端人如何保持竞争力?
查看>>
【JavaScript 教程】面向对象编程——实例对象与 new 命令
查看>>
我在网易做了6年前端,想给求职者4条建议
查看>>
SQL1015N The database is in an inconsistent state. SQLSTATE=55025
查看>>
RQP-DEF-0177
查看>>
MySQL字段类型的选择与MySQL的查询效率
查看>>
Java的Properties配置文件用法【续】
查看>>
JAVA操作properties文件的代码实例
查看>>
java杂记
查看>>
RunTime.getRuntime().exec()
查看>>
Oracle 分组排序函数
查看>>
删除weblogic 域
查看>>
VMware Workstation 14中文破解版下载(附密钥)(笔记)
查看>>
日志框架学习
查看>>
日志框架学习2
查看>>
SVN-无法查看log,提示Want to go offline,时间显示1970问题,error主要是 url中 有一层的中文进行了2次encode
查看>>
NGINX
查看>>