Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:

  1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
  2. You could assume no leading zero bit in the integer’s binary representation.

Example 1:

Input: 5
Output: 2
Explanation: The binary representation of 5 is (no leading zero bits), and its complement is . So you need to output 2.

Example 2:

Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
 public class Solution {
public int findComplement(int num) {
int mask = (Integer.highestOneBit(num) << 1) - 1;
num = ~num;
return num & mask;
}
}
Integer.highestOneBit(num); //01101返回1000,001返回1,101返回100

为什么需要mask:

例如num = 0000 1010,那么 ~num = 1111 0101,加入mask = 0000 1111后,~num前面的1都消除了。

位运算(6)——Number Complement的更多相关文章

  1. 【leetcode】Single Number && Single Number II(ORZ 位运算)

    题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...

  2. 136.137.260. Single Number && 位运算

    136. Single Number 意思就是给你一堆数,每个数都出现了两次,只有一个数只出现了一次,找出这个数 位运算(和c艹一样) &:按位与 |:按位或 ^:异或(一样为0,不一样为1) ...

  3. HDU 3006 The Number of set(位运算 状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3006 题目大意:给定n个集合,每个集合都是由大于等于1小于等于m的数字组成,m最大为14.由给出的集合 ...

  4. LeetCode - 136. Single Number - ( C++ ) - 解题报告 - 位运算思路 xor

    1.题目大意 Given an array of integers, every element appears twice except for one. Find that single one. ...

  5. The number of set(位运算+状态dp)一道十分美妙的题目哦!!!!!!

    Given you n sets.All positive integers in sets are not less than 1 and not greater than m.If use the ...

  6. LeetCode 476. Number Complement

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  7. LeetCode_Easy_471:Number Complement

    LeetCode_Easy_471:Number Complement 题目描述 Given a positive integer, output its complement number. The ...

  8. 【LeetCode】位运算 bit manipulation(共32题)

    [78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...

  9. 【LeetCode】476. 数字的补数 Number Complement

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,476, 补数,二进制,Pyth ...

  10. js中的位运算

    按位运算符是把操作数看作一系列单独的位,而不是一个数字值.所以在这之前,不得不提到什么是"位": 数值或字符在内存内都是被存储为0和 1的序列,每个0和1被称之为1个位,比如说10 ...

随机推荐

  1. php常用的系统函数大全

    字符串函数 strlen:获取字符串长度,字节长度 substr_count 某字符串出现的次数 substr:字符串截取,获取字符串(按照字节进行截取) mb_strlenmb_substr str ...

  2. js Array属性和用法

    //构造函数创建数组 var arr1=new Array(); //字面量的方式 var arr2=[]; 1.Array.isArray(对象)---->判断这个对象是不是数组 //判断对象 ...

  3. 【Linux】DNS服务-BIND基础配置

    1.BIND简介 现在使用最为广泛的DNS服务器软件是BIND(Berkeley Internet Name Domain),最早有伯克利大学的一名学生编写,现在最新的版本是9,有ISC(Intern ...

  4. springcloud微服务总结二 注册中心

    一:netflix和springcloud关系 netflix公司开源了很多组件,包括服务注册与发现(Netflix Eureka).断路器(Netflix Hystrix).负载均衡(Netflix ...

  5. python之控制流习题+代码

    # !/usr/bin/python3 # -*- coding: utf-8 -*- # @Time :2018/11/26 15:32 # @Author :yosef # @Email :wur ...

  6. UESTC - 1652 递推方程

    方程很简单,每一公里往上推就行 WA了2发,忘了单通道时的特判,还有n m傻傻分不清,忘了fixed什么的我好弱啊QAQ.. #include<bits/stdc++.h> #define ...

  7. UESTC - 618

    #include<bits/stdc++.h> using namespace std; const int maxn = 1e6+11; const int N = 1e6; typed ...

  8. ndoejs解析req,伪造http请求

    require("./m3m4") var http = require('http'); var server = http.createServer(); server.lis ...

  9. vim的多窗口功能与环境参数设置

    Vim的多窗口功能 多窗口情况下按键功能 :sp [filename] 打开一个新的窗口,如果有加filename,表示在新窗口打开一个新文件,否则表示两个窗口为同一文件内容 :[ctrl]+w+j( ...

  10. docker 部署disconf 以及将其做成镜像

    1.需要一台服务器(阿里云,腾讯云.实体服务器都行,本次是以实体服务器为依照做的) 2.安装docker   https://www.cnblogs.com/shijunjie/p/10436293. ...