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
getBit(
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
int
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
int
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
int
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
int
clearBitsIthrough0(intnum,
int i) {
    intmask = ~(1
<< (i + 1)) -
1;
    returnnum & mask;
}

Common Bit Tasks的更多相关文章

  1. What is Away3D

    做了几个基于Flash平台的3D的项目,一路走来收获颇多.Away3D作为一个开源的Flash3D引擎,在3D页游领域,无疑是当前OGRE在国内的地位. 翻译出了多年前做Away3D中国社区的时候翻译 ...

  2. DotNet 资源大全中文版(Awesome最新版)

    Awesome系列的.Net资源整理.awesome-dotnet是由quozd发起和维护.内容包括:编译器.压缩.应用框架.应用模板.加密.数据库.反编译.IDE.日志.风格指南等. 算法与数据结构 ...

  3. F#之旅1 - Why use F#?为什么要用F#?

    原文地址:http://fsharpforfunandprofit.com/why-use-fsharp/ Why use F#?Why you should consider using F# fo ...

  4. (转) 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 ...

  5. Play libs

    The play.libs package contains several useful libraries that will help you to achieve common program ...

  6. jQuery能做些什么

    来源于: Learning jQuery, 4th Edition What jQuery does: 1. Access elements in a document; $('div.content ...

  7. 初探YAML

    YAML何许物也?在XML泛滥的情况下,YAML的出现的确让人眼前一亮,在初步学习了YAML以后,粗略的总结了一下,拿出来和大家分享.[MindMap][参考文档]YAML Specification ...

  8. Java Garbage Collection Basics--转载

    原文地址:http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html Overview Purpose ...

  9. YAML初探

    http://www.cnblogs.com/chwkai/archive/2009/03/01/249924.html 1 概念YAML是一种人们可以轻松阅读的数据序列化格式,并且它非常适合对动态编 ...

随机推荐

  1. B/S与C/S架构

    1.CS.BS架构定义 CS(Client/Server):客户端----服务器结构.C/S结构在技术上很成熟,它的主要特点是交互性强.具有安全的存取模式.网络通信量低.响应速度快.利于处理大量数据. ...

  2. Python作业之多级菜单

    作业之多级菜单 菜单实现要求: 1. 列出菜单选择供选择 2. 选择对应内容进入下一级菜单 3. 任何时候都可以选择退出程序或返回上一级菜单 具体代码如下: goods = {'华为':{'A系':{ ...

  3. Python中dict的功能介绍

    Dict的功能介绍 1. 字典的两种函数(方法) 1. 字典的内置函数 包含关系 格式:x.__contains__(key)等同于key in x 例如:dic = {'ab':23,'cd':34 ...

  4. Linux服务器之间免密登录设置

    说明: A为linux服务器a B为linux服务器b 每台linux都有ssh的服务端和客户端,linux下的ssh命令就是一个客户端 我们常用ssh协议来进行登陆或者是文件的拷贝,都需要密码 A要 ...

  5. git reset揭秘

    一.命令 首先,让我们来解释几个定义.   HEAD(头)     指向当前branch最顶端的一个commit,该分支上一次commit后的节点   Index(索引)     The index, ...

  6. Luogu P1919 【模板】A*B Problem升级版(FFT快速傅里叶_FFT

    这其实就是一道裸的FFT 核心思想:把两个数拆成两个多项式用FFT相乘,再反序输出 py解法如下: input() print(int(input())*int(input())) 皮一下hihi f ...

  7. java绘图原理------在窗口界面(或面板上)画出一张或多张图片问题解决方法

    /** *@author blovedr * 功能: java绘图原理------在窗口界面(或面板上)画出一张或多张图片问题解决方法 * 日期: 2018年4月28日     16:20 * 注释: ...

  8. Junit4 java.lang.Exception: No runnable methods

    出现如下错误: java.lang.Exception: No runnable methods at org.junit.runners.BlockJUnit4ClassRunner.validat ...

  9. ACM 今年暑假不AC

    "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" "@#$%^&*%... ...

  10. 如何找出Xcode中不同版本Swift的路径

    我们知道Xcode中可能包含不知一个Swift的版本,那么我们如何找到它们对应的路径呢? 熟悉unix shell命令的童鞋都知道有一个find指令,在我们已知Xcode路径时,我们可以在其中找到Sw ...