腾讯面试题:位运算——用位存储40亿个40亿以内的整数
一、题目 1: #include <iostream> 2: using namespace std; 3: 4: typedef unsigned int Bit32; // 能表示42亿+的无符号整数 5: 6: const Bit32 maxLen=125000001; //数组长度,0-40亿 7: const Bit32 thirtytwo=32; 8: 9: void createArray(Bit32 *&arr) 10: { 11: arr=new Bit32[maxLen]; 12: memset(arr,(Bit32)0,sizeof(arr)); 13: } 14: 15: bool setBit(Bit32 *arr,Bit32 num) 16: { 17: if(((arr[num/thirtytwo])&(1<<(thirtytwo-num%thirtytwo-1)))==0) 18: { 19: arr[num/thirtytwo]|=1<<(thirtytwo-num%thirtytwo-1); 20: return true; 21: } 22: else 23: { 24: return false; 25: } 26: } 27: 28: int main() 29: { 30: Bit32 *s; 31: createArray(s); 32