Common Bit Tasks
1) If you XOR a bit with its own negated value, you will always get 1. Therefore thesolution to a ^ (~a) will be the sequence of 1s.
consider a is of data-type byte.
example:
a = 00000001
~a = 11111110
Now a ^ (~a) = 11111111
2) An operation like x & (~0 << n) clears the n rightmost bits of x. The value ~o is the sequences of 1s, so by shifting it by n, we have a bunch of ones followed by n zeros. By doing AND we clear the the rightmost n digits
of x.
3) Bit Facts and Tricks:
x ^ 0′s = x x & 0′s = 0 x | 0′s = x
x ^ 1′s = ~x x & 1′s = x x | 1′s = 1′s
x ^ x = 0 x & x = x x | x = x
4) Get Bit :
If I want to get the bit at i-th position, this method shifts1 over by i bits , creating a value and performing an
AND operation with the numbernum. If the resultant value is zero, that bit is unset else, that bit is set.
|
1
2
3
|
boolean int num,inti) { return((num & (1<< i)) != 0);} |
5) Set Bit:
If I want to set the bit at i-th position, this method shifts1 over by i bits , creating a value and performing an
OR operation with the numbernum.
|
1
2
3
|
public setBit(intnum,int i) { returnnum | (1<< i);} |
6) Clear Bit:
This method operates in reverse way as the setBit method works. To clear the
i-th bit we first negate the result obtained from 1 << i and perform AND operation with the numbernum.
|
1
2
3
4
|
public clearBit(intnum,int i) { intmask = ~(1<< i); returnnum & mask;} |
7) Clear all the MSB through i (inclusive) 高位到低位清零(包括低位)
This method is meant for clearing all the MSB’s (Most Significant Bits) through i (inclusive) we do:
|
1
2
3
4
|
public clearBitsMSBThrough(intnum,int i) { intmask = (1 << (i + 1) ) - 1; returnnum & mask;} |
8) Clear all the bits from i through o(inclusive) 低位到0位清零 (包括低位)
This method is meant for clearing all the bits from i through 0 from the number num:
|
1
2
3
4
|
public clearBitsIthrough0(intnum,int i) { intmask = ~(1<< (i + 1)) -1; returnnum & mask;} |
Common Bit Tasks的更多相关文章
- What is Away3D
做了几个基于Flash平台的3D的项目,一路走来收获颇多.Away3D作为一个开源的Flash3D引擎,在3D页游领域,无疑是当前OGRE在国内的地位. 翻译出了多年前做Away3D中国社区的时候翻译 ...
- DotNet 资源大全中文版(Awesome最新版)
Awesome系列的.Net资源整理.awesome-dotnet是由quozd发起和维护.内容包括:编译器.压缩.应用框架.应用模板.加密.数据库.反编译.IDE.日志.风格指南等. 算法与数据结构 ...
- F#之旅1 - Why use F#?为什么要用F#?
原文地址:http://fsharpforfunandprofit.com/why-use-fsharp/ Why use F#?Why you should consider using F# fo ...
- (转) The major advancements in Deep Learning in 2016
The major advancements in Deep Learning in 2016 Pablo Tue, Dec 6, 2016 in MACHINE LEARNING DEEP LEAR ...
- Play libs
The play.libs package contains several useful libraries that will help you to achieve common program ...
- jQuery能做些什么
来源于: Learning jQuery, 4th Edition What jQuery does: 1. Access elements in a document; $('div.content ...
- 初探YAML
YAML何许物也?在XML泛滥的情况下,YAML的出现的确让人眼前一亮,在初步学习了YAML以后,粗略的总结了一下,拿出来和大家分享.[MindMap][参考文档]YAML Specification ...
- Java Garbage Collection Basics--转载
原文地址:http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html Overview Purpose ...
- YAML初探
http://www.cnblogs.com/chwkai/archive/2009/03/01/249924.html 1 概念YAML是一种人们可以轻松阅读的数据序列化格式,并且它非常适合对动态编 ...
随机推荐
- js 输入密码框遇到的问题
上次公司项目需要用到密码输入框,如图所示: 首先设计是设置六个div,然后放置六个input. 动态方面然根据键盘onkey事件进行判断,当按键放松时使前一个input失去焦点,下一个input获取焦 ...
- jquery checkbox是否选中
$("#chkDisplayZxOnly").is(":checked") 选中返回true,否则返回false
- Flexible DEMO 实现手淘H5页面的终端适配
<!DOCTYPE html> <html> <head> <title>淘宝flexiblejs</title> <meta cha ...
- Ubuntu14.04安装 HP DeskJet GT 5820 打印机的方法
原创内容发表在 http://www.linuxdiyf.com/linux/25331.html 步骤就是: 这里(https://sourceforge.net/projects/hplip/fi ...
- DDD实战进阶第一波(八):开发一般业务的大健康行业直销系统(业务逻辑条件判断最佳实践)
这篇文章其实是大健康行业直销系统的番外篇,主要给大家讲讲如何在领域逻辑中,有效的处理业务逻辑条件判断的最佳实践问题. 大家都知道,聚合根.实体和值对象这些领域对象都自身处理自己的业务逻辑.在业务处理过 ...
- Python中的赋值(复制)、浅拷贝、深拷贝之间的区别
1.赋值: 只是复制了新对象的引用,不会开辟新的内存空间. 2.浅拷贝: 创建新对象,其内容是原对象的引用. 浅拷贝有三种形式:切片操作,工厂函数,copy模块中的copy函数. 如: ...
- 转:rabbitMQ 安装与管理
安装环境 虚拟机:VMware® Workstation 10.0.1 build Linux系统:CentOS6.5 官方安装:http://www.rabbitmq.com/install-rpm ...
- Go 语言常量
常量是一个简单值的标识符,在程序运行时,不会被修改的量. 常量中的数据类型只可以是布尔型.数字型(整数型.浮点型和复数)和字符串型. 常量的定义格式: const identifier [type] ...
- C++编译连接过程中关于符号表的报错分析
是这样的,在学习郑莉老师的多文件结构和编译预处理命令章节时候,看到书里有这么一张图描述如下:#include指令作用是将指定的文件嵌入到当前源文件中#include指令所在的位置. 然后我就想5_10 ...
- Conference-Web Search and Data Mining
Conference WSDM(Web Search and Data Mining)The ACM WSDM Conference Series 不像KDD.WWW或者SIGIR,WSDM因为从最开 ...