C/C++01: 善用位计算

原文地址: http://blog.csdn.net/morewindows/article/details/7354571

推荐书籍: Hacker‘s Delight

1.判断奇偶

1 if (a & 1)
2 {
3     printf("%i is an odd number", a);
4 }

2.交换两数

1 void Swap(int &a, int &b)
2 {
3     if (a != b)
4     {
5         a ^= b;
6         b ^= a;
7         a ^= b;
8     }
9 }

3.变换符号

1 int SignReversal(int a)
2 {
3     return ~a + 1;
4 }

4.求绝对值

1 int my_abs(int a)
2 {
3     int i = a >> 31;
4     return ((a ^ i) - i);
5 }

注:如果a是负数,那么a右移31位为-1

5.高低位交换

 1 #include <stdio.h>
 2
 3 template <class T>
 4 void PrintfBinary(T a)
 5 {
 6     int i;
 7     for (i = sizeof(a) * 8 - 1; i >= 0; --i)
 8     {
 9         if ((a >> i) & 1)
10             putchar(‘1‘);
11         else
12             putchar(‘0‘);
13         if (i == 8)
14             putchar(‘ ‘);
15     }
16     putchar (‘\n‘);
17 }
18
19 int main()
20 {
21     printf("Before: ");
22     unsighed short a = 12345;
23     PrintfBinary(a);
24
25     printf("After: ");
26     a = (a >> 8) | (a <<8);
27     PrintfBinary(a);
28     return 0;
29 }

6.二进制逆序

 1 #include <stdio.h>
 2 int main()
 3 {
 4     printf("Before: ");
 5     unsigned short a = 12345;
 6     PrintfBinary(a);
 7
 8     printf("After: ");
 9     a = ((a & 0xAAAA) >> 1) | ((a & 0x5555) << 1);
10     a = ((a & 0xCCCC) >> 2) | ((a & 0x3333) << 2);
11     a = ((a & 0xF0F0) >> 4) | ((a & 0x0F0F) <<4);
12     a = ((a & 0xFF00) >> 8) | ((a & 0x00FF) <<8);
13     PrintfBinary(a);
14
15     return 0;
16 }

7.二进制中1的个数

 1 #include <stdio.h>
 2 int main()
 3 {
 4     unsigned short a = 12345;
 5     a = ((a & 0xAAAA) >> 1) + (a & 0x5555);
 6     a = ((a & 0xCCCC) >> 2) + (a & 0x3333);
 7     a = ((a & 0xF0F0) >> 4) + (a & 0x0F0F);
 8     a = ((a & 0xFF00) >> 8) + (a & 0x00FF);
 9     printf("Have %i ones in binary", a);
10     return 0;
11 }
时间: 2024-08-09 14:42:21

C/C++01: 善用位计算的相关文章

CodeBlocks16.01 MinGW32位 配置SDL2.0.4

首先先从官网https://www.libsdl.org/下载开发库SDL2-devel-2.0.4-mingw.tar.gz (MinGW 32/64-bit). 解压后,看到i686-w64-mingw32(32位)和x86_64-w64-mingw32(64位)这两个文件夹. 将i686-w64-mingw32\include里的SDL2文件夹复制到CodeBlocks\MinGW\include中,i686-w64-mingw32\lib里的*.a文件复制到CodeBlocks\MinG

hdu 4901 The Romantic Hero 计数dp,位计算

The Romantic Hero Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1128    Accepted Submission(s): 469 Problem Description There is an old country and the king fell in love with a devil. The d

ZOJ 3812 We Need Medicine 01背包+位优化

题目链接:点击打开链接 #include <cstdio> #include <algorithm> #include <iostream> #include <cstring> #include <map> typedef unsigned long long ll; using namespace std; const int N = 400 + 1; const int M = 200000 + 1; int t[N], w[N], g[5

HDU3810 Magina(搜索+用优先队列模拟01背包)经典

Magina Time Limit: 60000/30000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 528    Accepted Submission(s): 177 Problem Description Magina, also known as Anti-Mage, is a very cool hero in DotA (Defense of the Anci

【转】在64位机上跑32位程序注意事项

原文网址:http://blog.chinaunix.net/uid-20742320-id-4744472.html 今天在交叉编译一个应用程序时,发现porting到板子上比较麻烦(nfs还没有支持),想现在PC上调试. 于是在板子上编译好,运行,发现指定的网页打不开,以为是httpd的confi有些出入,检查了下,没有错误. 在一筹莫展之际,直接在pc上运行突然发现出现segmdefault. segmdefault又是如何出现的呢? 通过file命令查看目标文件是elf64 而reade

php中浮点数计算问题

如果用php的+-*/计算浮点数的时候,可能会遇到一些计算结果错误的问题,比如echo intval( 0.58*100 );会打印57,而不是58,这个其实是计算机底层二进制无法精确表示浮点数的一个bug,是跨语言的,我用python也遇到这个问题.所以基本上大部分语言都提供了精准计算的类库或函数库,比如php有BC高精确度函数库,下面达内php培训老师介绍一下一些常用的BC高精确度函数使用. 例子  代码如下   <?php    $f = 0.58;    var_dump(intval(

计算结构体大小

char类型的长度被定义为一个8位字节,这很简单. short类型的长度至少为两字节.在有些计算机上,对于有些编译程序,short类型的长度可能为4字节,或者更长. int类型是一个整数的“自然”大小,其长度至少为两字节,并且至少要和short类型一样长.在16位计算机上,int类型的长度可能为两字节:在32位计算机上,可能为4字节:当64位计算机流行起来后,int类型的长度可能会达到8字节.这里说的都是“可能”,例如,早期的Motorala 68000是一种16/32位的混合型计算机,依赖于不

利用位运算进行权限管理

原理 在Linux文件系统中,一个用户对文件或目录所拥有的权限分为三种:”可读”.”可写”和”可执行”,分别用 1 .2 和 4 来表示,它们之间可以任意组合:有“可读”.“可写”权限就用 3 来表示(1 + 2 = 3):有”可读“.”可执行“权限就用5来表示(1 + 4 = 5),三种权限全部拥有就用 7 表示(1 + 2 + 4 = 7). 实际上,这种运算是基于二进制的. 假设可执行.可写.可读三种权限分别对应三个状态位,如果用户具有某种权限,那么将对应的状态位标识为“1”,反之则标识为

django部署到apache上(非常重要的,3者版本要一致,是32位就都要是32位的)

网上把django部署到apache的文章挺多的,但是按照大家的操作,并没有一次就成功,还是遇到了一些问题,这里主要有以下几个情况. 1.网上找到的mod_wsgi的版本问题,导致动态库加载不上. 2.配置问题,因为涉及到apache.Python和mod_wsgi的版本,所以配置上可能也不太一样. 这里我把我遇到的问题和解决方案比较详细的写下来.失败经历可跳过. 失败经历 我的环境是python2.7.11 64位版本,apache我选择的是2.4.20x 64位版本,这里有一点必须要保证的是