Bits Facts and Tricks

x ^ 0s =  x

x & 0s =  0

x | 0s = x

x ^ 1s = ~x

x & 1s = x

x | 1s = 1s

x ^ x = 0

x & x = x

x | x = x

Common Bit Tasks:  Get, Set, Clear And Update

Get:  num & (1 << i) != 0

Set: num | (1 << i)

Clear:

clear ith bit: num & (~(1 << i))

clear all bits from the most significant bit through i: num & ((1 << i) - 1)

clear all bits from i though 0: num & (~((1 << (i + 1)) - 1))

Update: (num & (~(1 << i))) | (v << i)

5.4

 ((n & (n - 1)) == 0) //check if n is a power of 2 (or if n is 0)

5.5 Swap odd and even bits in an integer with as few instructions ad possible.

 //0xaaaaaa is 1010101010 which mask all odd bits
public int swapOddEven(int x){
return ( ((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1) );
}

Chp5: Bit Manipulation的更多相关文章

  1. backup, file manipulation operations (such as ALTER DATABASE ADD FILE) and encryption changes on a database must be serialized.

    昨天在检查YourSQLDba备份时,发现有台数据库做备份时出现了下面错误信息,如下所示: <Exec>   <ctx>yMaint.ShrinkLog</ctx> ...

  2. Hololens开发笔记之Gesture手势识别(Manipulation手势控制物体旋转)

    Manipulation gesture:保持点击手势,在3D世界中绝对运动 当你想要全息图像1:1响应用户手部移动时,操纵手势能被用于移动.缩放或旋转全息图像.如此的一个用处是使得用户可以在世界中绘 ...

  3. Hololens开发笔记之Gesture手势识别(Manipulation手势控制物体平移)

    Manipulation gesture:保持点击手势,在3D世界中绝对运动 当你想要全息图像1:1响应用户手部移动时,操纵手势能被用于移动.缩放或旋转全息图像.如此的一个用处是使得用户可以在世界中绘 ...

  4. Track files and folders manipulation in Windows

    The scenario is about Business Secret and our client do worry about data leakage. They want to know ...

  5. Data manipulation primitives in R and Python

    Data manipulation primitives in R and Python Both R and Python are incredibly good tools to manipula ...

  6. VK Cup 2012 Qualification Round 2 C. String Manipulation 1.0 字符串模拟

    C. String Manipulation 1.0 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/pr ...

  7. Bash String Manipulation Examples – Length, Substring, Find and Replace--reference

    In bash shell, when you use a dollar sign followed by a variable name, shell expands the variable wi ...

  8. windows phone 之手势识别(Manipulation)

    在Windows Phone 7的多触摸屏上可以检测到至少四根同时存在的手指,并且一起操作使触摸屏充分发挥效果. 在silverlight开发中通过事件来实现触屏事件的检测,包括低级别的和高级别的接口 ...

  9. WPF Multi-Touch 开发:高级触屏操作(Manipulation)

    原文 WPF Multi-Touch 开发:高级触屏操作(Manipulation) 在上一篇中我们对基础触控操作有了初步了解,本篇将继续介绍触碰控制的高级操作(Manipulation),在高级操作 ...

随机推荐

  1. 转帖:使用TortoiseGit处理代码冲突

    原址:http://www.cnblogs.com/jason-beijing/p/5718190.html   场景一  user0 有新提交 user1 没有pull -> 写新代码 -&g ...

  2. Linux 本地yum源搭建和网络yum源搭建

    一.本地yum源搭建 首先挂载上光盘 [root@www /]# mount /dev/cdrom /media/cdrom/ 系统默认已经安装了可使用yum的软件包,所以可以直接配置: [root@ ...

  3. 【全面解析DeepZoom 之三】建立DeepZoom应用

    文章出处:http://www.cnblogs.com/zhouyinhui/archive/2008/04/14/1153371.html (周银辉) 与导出整图不一样,你不能这样使用: <M ...

  4. windows不能在本地计算机启动apache

    今天,配置eclipse PHP studio 3.0的时候更改了apache http server 中的httpd.conf文件: 将DocumentRoot 的路径设错了,为一个不存在目录 .更 ...

  5. php随机验证码

    今天同学问我,用php怎么写验证码,由于是新手所以花了半天的时间才完成.而且功能很是简单呵呵.今天本来打算写session和cookie的看来是要明天了. <?php $image_width= ...

  6. js中(function(){…})()立即执行函数写法理解(转载oschina)

    ( function(){…} )()和( function (){…} () )是两种javascript立即执行函数的常见写法,最初我以为是一个括号包裹匿名函数,再在后面加个括号调用函数,最后达到 ...

  7. trade 主要前端组件

    jQuery Custombox http://www.jqueryfuns.com/resource/view/27

  8. RichTextBox 自动滚动到最后

    RichTextBox.AppendText($"[{DateTime.Now.ToString("hh:mm:ss")}] {msg}\n"); RichTe ...

  9. 百度云demo2

  10. 无法访问Fedora的samba共享

    配置好samba服务后,却发现windows无法访问.经过多次试验与fedora的防火墙有关系. 关闭防火墙: #service iptables stop 或清空规则: #iptables -F w ...