Q5.6
来源:互联网 发布:linux日志管理系统 编辑:程序博客网 时间:2024/06/10 15:22
#include <iostream>using namespace std;void print_binary(int x){ string s = ""; for(int i=0; i<32 && x!=0; ++i, x >>= 1){ if(x&1) s = "1" + s; else s = "0" + s; } cout<<s<<endl;}int bit_swap(int a){return (a & 0x55555555) << 1 | (a & 0xaaaaaaaa) >> 1; }int main(void){int a = -2;cout << "a:" << a << endl; print_binary(a);cout << "after bit_swap: " << bit_swap(a) << endl;print_binary(bit_swap(a));cout << "*************************************************" << endl << endl;//int test = -2;//print_binary(test);//cout << test << endl << endl;cout << "间接输出: " << endl;cout << "1. c = -2 & 0xaaaaaaaa" << endl;int c = -2 & 0xaaaaaaaa;print_binary(c);//cout << c << endl << endl;cout << "2. d = c >> 1" << endl;int d = c >> 1;print_binary(c >> 1); cout << endl; //有符号数右移,高位补1 //cout << d << endl;//cout << bit_swap(a) << endl;cout << "直接输出:(-2 & 0xaaaaaaaa) >> 1" << endl; //仅有31位,说明最高位为0,验证了Hawstain的说法:(x & 0xAAAAAAAA) 得出的结果是无符号数 //无符号数右移,最高位补0 print_binary((-2 & 0xaaaaaaaa) >> 1);return 0;}
结果如下:
0 0
- Q5.6
- Q5
- Q5
- Cracking the coding interview--Q5.6
- Cracking the coding interview--Q5.6
- Q5.6 swap odd and even bits
- Q5.2
- Q5.3
- Q5.4
- Q5.7
- Leetcode Q5
- 出售贝贝棋牌游戏分Q5 6 4 4 8 2 9 9
- Q5 营销组合分析
- Dream Car 奥迪Q5
- [Python Challenge] - Q5
- Q5+maxim dl
- Project Euler Q5 解法
- Q5.1 Set Bits
- 四个开源商业智能平台比较(六)
- OCP 1Z0 053 63
- UICollectionView介绍
- epoll的惊群效应
- hadoop 在eclipse中输出日志
- Q5.6
- sscanf - scanf,sscanf高级用法
- 在文本文件中输出宽中文字符
- JNI 详解
- 深入理解iPhone数据持久化
- android,eclipse中的logcat详解
- 给你一个亿-电视节目总结
- Apache配置文件详解
- 奇葩的table里<th>和<td>不等宽问题