Lintcode: Flip Bits

Determine the number of bits required to flip if you want to convert integer n to integer m.

Have you met this question in a real interview? Yes
Example
Given n = 31 (11111), m = 14 (01110), return 2.

Note
Both n and m are 32-bit integers.

This is to test XOR(called: exclusive or)

Notice "=="‘s order higher than "&"

 1 class Solution {
 2     /**
 3      *@param a, b: Two integer
 4      *return: An integer
 5      */
 6     public static int bitSwapRequired(int a, int b) {
 7         // write your code here
 8         int res = 0;
 9         int xor = a ^ b;
10         for (int i=0; i<32; i++) {
11             if (((xor>>i)&1) == 1)
12                 res++;
13         }
14         return res;
15     }
16 };
时间: 2025-01-15 06:26:48

Lintcode: Flip Bits的相关文章

LintCode刷题笔记--Flip Bits

Flip Bits: 题目:Determine the number of bits required to flip if you want to convert integer n to integer m. 解题思路: 给出两个数字a和b,返回两个数字中需要转换的内容这道题主要是考察位运算的几种操作:1.首先使用异或运算来确定在哪些位置上两个数字的二进制位不一样的数字上都填上1,得到bit=a^b. 2. 之后在与1进行与运算,如果bit的最后一位是1那么就得到1,否则为03. 将bit向

[Lintcode]181. Flip Bits

181. Flip Bits 本题难度: Easy Topic: Math&Bit Manipulation Description Determine the number of bits required to flip if you want to convert integer n to integer m. Example Given n = 31 (11111), m = 14 (01110), return 2. Notice Both n and m are 32-bit int

181. Flip Bits【LintCode, by java】

Description Determine the number of bits required to flip if you want to convert integer n to integer m. Both n and m are 32-bit integers. Example Given n = 31 (11111), m = 14 (01110), return 2. 解题:比较两个整数对应的二进制数,共有多少位不同.注意,负数也包含在内.">>>"

Flip Bits

Determine the number of bits required to flip if you want to convert integer n to integer m. Notice Both n and m are 32-bit integers. Example Given n = 31 (11111), m = 14 (01110), return 2. 1 class Solution { 2 /** 3 *@param a, b: Two integer 4 *retu

Lintcode: Update Bits

Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e g , M becomes a substring of N located at i and starting at j) Have you met this question in a real interview? Yes Ex

debugfs linux rm 删除 恢复 Attempt to read block from filesystem resulted in short read while opening filesystem

sudo apt-get install foremost w debugfs: ? Available debugfs requests: show_debugfs_params, params Show debugfs parameters open_filesys, open Open a filesystem close_filesys, close Close the filesystem freefrag, e2freefrag Report free space fragmenta

【ThinkingInC++】73、深入理解模板

第五章 深入理解模板 5.1 模板参数 关于bitset bitset就是可以存放二进制的容器. 对于bitset的主要操作有: (constructor) Construct bitset (public member function)    //构造bitset..   格式bitset<长度>  名字 applicable operators Bitset operators (functions)          //可以直接对bitset容器进行二进制操作,如^,|,~,<

Flip the Bits(思维)

You are given a positive integer n. Your task is to build a number m by flipping the minimum number of bits in the binary representation of n such that m is less than n (m < n) and it is as maximal as possible. Can you? Input The first line contains

lintcode-easy-Flip Bits

Determine the number of bits required to flip if you want to convert integer n to integer m. Example Given n = 31 (11111), m = 14 (01110), return 2. Note Both n and m are 32-bit integers. class Solution { /** *@param a, b: Two integer *return: An int