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. 函数的形参和实参之arguments对象

    当函数调用函数时候传入的实参比函数声明时候制定的形参要少时候,剩余的形参就设置成了undefined.例如 function getPropertyNames(o,/*optional*/a){ va ...

  2. 46. Permutations(medium, backtrack, 重要)

    Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have ...

  3. 设计模式之中介者模式(Mediator )

    中介者模式是关于数据交互的设计模式,该模式的核心是一个中介者对象,负责协调一系列对象之间的不同的数据请求,这一系列对象成为同事类.如房产中介(简直不想提它),买房的卖房的,租房的放租的都到房产中介那里 ...

  4. DOS/Windows 文本格式与 Unix 文本各式转换

    命令简介: dos2unix是将Windows格式文件转换为Unix.Linux格式的实用命令.Windows格式文件的换行符为\r\n ,而Unix&Linux文件的换行符为\n. dos2 ...

  5. Go 语言函数

    函数是基本的代码块,用于执行一个任务. Go 语言最少有个 main() 函数. 你可以通过函数来划分不同功能,逻辑上每个函数执行的是指定的任务. 函数声明告诉了编译器函数的名称,返回类型,和参数. ...

  6. 前端技术之_CSS详解第二天

    前端技术之_CSS详解第二天 1.css基础选择器 html负责结构,css负责样式,js负责行为. css写在head标签里面,容器style标签. 先写选择器,然后写大括号,大括号里面是样式. & ...

  7. 初识Spark2.0之Spark SQL

    内存计算平台spark在今年6月份的时候正式发布了spark2.0,相比上一版本的spark1.6版本,在内存优化,数据组织,流计算等方面都做出了较大的改变,同时更加注重基于DataFrame数据组织 ...

  8. Docker学习笔记1:CentOS7 下安装Docker

    本文内容摘自官网:https://docs.docker.com/engine/installation/linux/centos/#/create-a-docker-group 注:本文是介绍Lin ...

  9. shiro salt

    1.1 散列算法 散列算法一般用于生成一段文本的摘要信息,散列算法不可逆,将内容可以生成摘要,无法将摘要转成原始内容.散列算法常用于对密码进行散列,常用的散列算法有MD5.SHA.分享牛系列,分享牛专 ...

  10. 等价于n*n的矩阵,填写0,1,要求每行每列的都有偶数个1 (没有1也是偶数个),问有多少种方法。

    #define N 4 /* * 公式: * f(n) = 2^((n - 1) ^2) */ int calWays(int n) { int mutiNum = (n - 1) * (n - 1) ...