位运算
SYaoJun
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
整数与IP地址之间的转换
题目 类型:位运算 难度:简单 注意:IP可能超过int需要用无符号整型,并且牛客的判题系统while中的scanf必须要不等于EOF不然会出错。 #include <cstdio> #include <iostream> #include <cstring> using namespace std; int main(){ unsigned int...原创 2020-04-11 08:40:35 · 341 阅读 · 0 评论 -
231. Power of Two
x&-x 常用在树状数组中 class Solution { public: bool isPowerOfTwo(int n) { return n > 0 && (n & -n) == n; } };原创 2019-09-14 07:55:17 · 139 阅读 · 0 评论 -
762. Prime Number of Set Bits in Binary Representation
位运算+模拟 class Solution { public: int countPrimeSetBits(int L, int R) { unordered_set<int> primes({2, 3, 5, 7, 11, 13, 17, 19}); int res = 0; for(int i = L; i <= R;...原创 2019-09-14 08:11:56 · 169 阅读 · 0 评论 -
136. Single Number
class Solution { public: //a ^ a = 0 //0 ^ a = a int singleNumber(vector<int>& nums) { int res = 0; for(auto x: nums){ res ^= x; } ...原创 2019-09-14 08:18:19 · 142 阅读 · 0 评论 -
476. Number Complement
class Solution { public: int findComplement(int num) { int res = 0, t = 0; while(num){ res += !(num &1) << t; //如果是0则取反后把1右移t位 num >>= 1; ...原创 2019-09-14 08:28:16 · 152 阅读 · 0 评论 -
奇偶位交换
大神代码 class Exchange { public: int exchangeOddEven(int x) { int oddVal = (x & 0xAAAAAAAA); // bit 1 3 5 ... 31 int evenVal = (x & 0x55555555); // bit 0 2 4 ... 30 re...原创 2019-09-19 17:27:02 · 223 阅读 · 0 评论 -
整数转换
class Transform { public: int calcCost(int x, int y) { string a = bitset<32>(x).to_string(); string b = bitset<32>(y).to_string(); int cnt = 0; for(int i =...原创 2019-09-19 17:33:26 · 320 阅读 · 0 评论 -
不用额外变量交换两个整数的值
x^x = 0 0^x = x 异或位运算 #include <cstdio> #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; int main(){ int m, n; cin>&...原创 2019-09-30 15:09:14 · 187 阅读 · 0 评论 -
整数的二进制数表达中有多少个1
#include <cstdio> #include <iostream> #include <vector> #include <string> #include <bitset> #include <algorithm> using namespace std; int main(){ int x; cin>&g...原创 2019-10-01 08:07:51 · 162 阅读 · 0 评论
分享