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是一种人们可以轻松阅读的数据序列化格式,并且它非常适合对动态编 ...
随机推荐
- 用burpsuite暴力破解后台
[实验原理] Burp Suite是Web应用程序测试的最佳工具之一,其多种功能执行各种任务.请求的拦截和修改,扫描web应用程序漏洞,以暴力破解登录表单,执行会话令牌等多种的随机性检查. Burp ...
- linux系统下安装jdk,mysql,tomcat 和redis 和jedis入门案例
Day47笔记Linux+redis入门 Day47 知识讲解:Jedis 1.Linux上jdk,mysql,tomcat安装(看着文档安装) 准备工作: 因为JDK,TOMCAT,MYSQL的 ...
- 浏览器本地下拉框查询选择js
首先需要引用jquery-1.7.2.js. 页面下拉框有对应的数据,此下拉框的查询将不与服务器交互.本地下拉框查询.暂不支持通过键盘上下按键和enter键控制 // JavaScript Docum ...
- redis在java客户端的操作
redis高性能,速度快,效率高的特点,用来做缓存服务器是很不错的选择.(和memcache相似)redis在客户端的操作步骤: 1.redis单机版操作 1.1通过Jedis对象操作 (1)将安装r ...
- 高效update方案
--方案1:如果有索引,先把索引删除后,再update,最后把索引重新创建一下因为索引对update影响很大. --方案2:1.create table newA as select id,name, ...
- 创建OpenGL Context(WGL)
创建OpenGL Context(WGL) 创建OpenGL Context是初始化OpenGL的一部分.只有在此之后才能使用OpenGL. 关于platform的注意事项 创建OpenGL cont ...
- 0428css样式
CSS样式表|-引入的三种方式|--内联样式|----标签内部(空格style)|--内嵌样式|----<head></head>标签内部(<style></ ...
- 初识在Spring Boot中使用JPA
前面关于Spring Boot的文章已经介绍了很多了,但是一直都没有涉及到数据库的操作问题,数据库操作当然也是我们在开发中无法回避的问题,那么今天我们就来看看Spring Boot给我们提供了哪些疯狂 ...
- Errors occurred during the build. Errors running builder 'JavaScript Validator' on project '项目名'.
把JavaScript Validator去掉.去掉的方法是:选择一个项目--右键Properties--Builders(排第二)--点一下右侧会有四项--取消第一项"JavaScript ...
- JVM性能参数调优实践,不会执行Full GC,网站无停滞
原文来自:http://bbs.csdn.net/topics/310110257 本文只做整理记录,供个人学习. 1 JVM参数调优是个很头痛的问题,设置的不好,JVM不断执行Full GC,导致整 ...