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, int i) { 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( int num, int i) { return num | ( 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( int num, int i) { int mask = ~( 1 << i); return num & 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( int num, int i) { int mask = ( 1 << (i + 1 ) ) - 1 ; return num & 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( int num, int i) { int mask = ~( 1 << (i + 1 )) - 1 ; return num & 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是一种人们可以轻松阅读的数据序列化格式,并且它非常适合对动态编 ...
随机推荐
- URL、网址、域名
URL (Uniform Resource Locator)统一资源定位符是对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址.互联网上的每个文件都有一个唯一的URL ...
- Oracle的dual
1.dual 确实是一张表.是一张只有一个字段,一行记录的表. 2.习惯上,我们称之为'伪表'.因为他不存储主题数据.3.他的存在,是为了操作上的方便.因为select 都是要有特定对象的.但如果我们 ...
- Java instanceof 关键字是如何实现的?
作者:RednaxelaFX链接:https://www.zhihu.com/question/21574535/answer/18998914来源:知乎著作权归作者所有.商业转载请联系作者获得授权, ...
- 用JavaScript按一定格式解析出URL 串中所有的参数
1.先看看location对象 2.其中的search属性就获取当前URL的查询部分(问号?之后的部分) 3.改造location.search 比如当前URL为:https://www.hao123 ...
- SpringMVC注解@Component、@Repository、@Service、@Controller区别
SpringMVC中四个基本注解: @Component.@Repository @Service.@Controller 看字面含义,很容易却别出其中三个: @Controller 控制层, ...
- 有趣的冷知识:编程中Foo, Bar 到底什么意思?
转自:编程中Foo, Bar 到底什么意思? 1 前言 在很多国外计算机书本和一些第三份开源软件的Demo中经常用到两个英文单词Foo,Bar.这到底是什么意思呢?从步入屌丝界的IT生活见到这两个单词 ...
- 高效update方案
--方案1:如果有索引,先把索引删除后,再update,最后把索引重新创建一下因为索引对update影响很大. --方案2:1.create table newA as select id,name, ...
- DotnetSpider (二) Downloader的设置 Request自定义数据字典
本篇主要分享自定义Downloader和Request信息,实现自定义请求内容,及将自定义内容存储. ** 温馨提示:如需转载本文,请注明内容出处.** 本文连接:http://www.cnb ...
- Bootstrap3 排版-地址
让联系信息以最接近日常使用的格式呈现.在每行结尾添加 可以保留需要的样式. Twitter, Inc. 795 Folsom Ave, Suite 600 San Francisco, CA 9410 ...
- hadoop入门级总结二:Map/Reduce
在上一篇博客:hadoop入门级总结一:HDFS中,简单的介绍了hadoop分布式文件系统HDFS的整体框架及文件写入读出机制.接下来,简要的总结一下hadoop的另外一大关键技术之一分布式计算框架: ...