逻辑运算小结1---和X拥有的1的个数相同,但是比X大的第一个数的求解方法
2016-12-12 22:17
239 查看
#include <iostream> using namespace std; //和X拥有的1的个数相同,但是比X大的第一个数的求解方法: unsigned snoob(unsigned x) //举例: X = 0000 1111 00000 { unsigned smallest, ripple, ones; // x = 0000 1111 0000 smallest = x & -x; // 0000 0001 0000 ripple = x + smallest; // 0001 0000 0000 ones = x ^ ripple; // 0001 1111 0000 ones = (ones >> 2) / smallest; // 0000 0000 0111 return ripple | ones; // xxx1 0000 0111 } int main() { unsigned int x; cin>>x; cout<<snoob(x)<<endl; return 0; }
相关文章推荐
- 《人工智能——一种现代方法》(第二版)【第3章 用搜索法对问题求解】小结
- 位运算的方法,小结
- java代码执行字符串中的逻辑运算方法
- 垂直三栏布局拥有相同高度的方法
- Ruby中的逻辑运算以及符号做为方法名
- js方法名不同但是操作相同,这类方法整合
- 计算机求解可溢出的两个整型数相乘,算数运算溢出解决方法
- 离散--第一节--逻辑符号 + 集合及其运算 + 证明方法概述
- Linux下求差集运算方法小结
- java代码执行字符串中的逻辑运算方法
- 小结在一个java源文件当中可以有多个类,但是为什么只能有一个public类呢?而当这个类被修饰为public的话,为什么源文件名必须要与类名相同呢?
- 逻辑位运算小结3——求x|y最小值
- 求解:在springMVC中,怎么根据参数来调用不同的方法?!但是,不是用if...else...!!!
- Linux Bash算数运算方法小结
- 算法--两种方法实现一个Add函数,让两个整数相加,但是不能使用+、-、*、/等四则运算符。
- 字符串四则运算表达式的求解(中缀表达式实现,模拟笔算的方法)
- CF209 Div2 (D) 找最长的连续子序列拥有相同的最大公约数,并且最大公约数是这个序列中的一个数-------左右延伸的方法
- 一个袋子里面有n个球,每个球上面都有一个号码(拥有相同号码的球是无区别的)。如果一个袋子是幸运的当且仅当所有球的号码的和大于所有球的号码的积。 例如:如果袋子里面的球的号码是{1, 1, 2, 3},这个袋子就是幸运的,因为1 + 1 + 2 + 3 > 1 * 1 * 2 * 3 你可以适当从袋子里移除一些球(可以移除0个,但是别移除完),要使移除后的袋子是幸运的。现在让你编程计算一下你可以获得
- 对于三进制计算机逻辑运算方法的猜想
- JQuery获得input ID相同但是type不同的方法