Bit Manipulation

转自:http://blog.csdn.net/xsloop/article/details/47006241

一共五中运算: 与,或,异或,左移,右移

 

常用技巧:
(1)n & (n-1)能够消灭n中最右侧的一个1。
(2) 右移:除以2, 左移:乘以2。
(3)异或性质:交换律,0^a=a, a^a=0;
(3)将常用字符、数字等均转为按位运算,可以节约空间。
 
leetcode 题目解析:
  1. 使用右移。
  2. 使用n&(n-1)可以消灭一个1的性质来求解

231 Power of Two  

      使用n&(n-1)=0来判断。
                注意0和负数的情况。
 
 190 Reverse Bits  
      使用右移和左移。
 
      
  136 Single Number 
 
 
   78  Subsets 
 
 
 
 
 
 
posted @ 2016-10-25 08:39  morningdew  阅读(139)  评论(0编辑  收藏  举报