[Binary conversion] Decimal to binary (including related question types)

insert image description here

Personal brief introduction: Rising star creator in the Java field; Alibaba cloud technology blogger, star blogger, expert blogger; on the way of learning Java, recording the learning process~ Personal homepage: .29.'s blog
Learning community
: Go in and have a stroll~

1. Introduction to the & operator

& 运算符, also known as the bitwise AND operator, is a bitwise operator. It is used to perform bitwise operations on two integers and produce a new integer in which the value at each corresponding bit is 1 only if the corresponding bit of both operands is 1, otherwise it is 0.

The bitwise AND operator “&”is represented using symbols. It compares each bit of the two operands bit by bit and produces the result according to the following rules:

  • If the corresponding bit of both operands is 1, the corresponding bit of the result is 1.
  • If one of the corresponding bits of the two operands is 0, the corresponding bit of the result is 0.

In other words, the result is 1 only if the corresponding bits in both operands are 1. Otherwise, the result is 0.


示例:

Suppose we have two binary numbers: 10111001 and 11001100. Using the bitwise AND operator to perform bitwise operations on them, the result is 10001000, which is the value obtained by bitwise ANDing the corresponding bits of the two operands.

  10111001 
& 11001100
  10001000



2. << operator introduction

<< 运算符is a bitwise left shift operator used to shift all the bits of an integer to the left by the specified number of bits.

The left shift operator shifts all bits of a binary number to the left and fills it with zeros on the right. Each left shift operation multiplies the operand by 2 to the nth power , where n is the number of bits to shift left.


示例

Suppose we have an integer x = 5, represented as 00000101 in binary, we can shift it left by two places using the left shift operator, resulting in 00010100, represented as 20 in decimal.

x = 5 << 2;



3. Convert decimal to binary

public class s01 {
    
    
	//转换、打印
	public static void print(int num) {
    
    
        
		//遍历32位(int在底层就是通过32位二进制存储的。)
		for(int i = 31;i >= 0;--i) {
    
    
			//通过 &运算 获得num的二进制形式
            //1 << 1 —— 二进制:0010
            //1 << 2 —— 二进制:0100
            //1 << 3 —— 二进制:1000
			System.out.print((num & (1 << i)) == 0?"0":"1");
		}
		System.out.println();
	}

Notice:

    1. Int is stored in 32-bit binary at the bottom layer.



4. Interview question 05.06. Integer conversion - LeetCode

题目
insert image description here


作答

class Solution {
    
    
    public int convertInteger(int A, int B) {
    
    
        int ans = 0;

        //遍历获取二进制形式的32个数位
        for(int i = 0;i < 32;++i){
    
    
            int a = (A & (1 << i));
            int b = (B & (1 << i));
            //遇到不相同的数位,就是需改变的数位
            if(a != b) ++ans;
        }

        return ans;
    }
}

insert image description here




insert image description here

Guess you like

Origin blog.csdn.net/ebb29bbe/article/details/132022022