Share some questions about bits

I accidentally brushed for a maintenance session today when i was brushing too hard. Good guys, it’s really bloody, but all in all, surgery is a really good way to solve the problem. Simple, convenient, fast, and fortunately, it is really difficult to understand at first, but as long as you get past the curve, it will be a good understanding. The ideas are relatively clear. Let me talk about the situation I’m facing. Process name.

  • No. 1 first place

Write a function whose input is an unsigned integer (in the form of a binary string) and returns the number of numeric digits in its binary expression in “1” (also known as Hanming weight).

Вход: 00000000000000000000000000001011
 Вывод: 3
 Объяснение: Во входной двоичной строке 000000000000000000000000001011, в общей сложности три цифры - «1».

I used two methods to solve this issue.
Let’s look at violence to decide:

public class Solution {
    public int hammingWeight(int n) {
        String s = Integer.toBinaryString(n);
        int count=0;
        for(int i=0;i<s.length();i++){
            if(s.charAt(i) == '1'){
        return count;

The solution to violence is very simple. It consists in converting this string of binary numbers into a string and traversing this string. If there is a 1, then we add one and finally return to the slope line to give the number from 1 that contains 1
The second way is to solve the process:

public class Solution {
    public int hammingWeight(int n) {
        int count = 0;
        int mask = 1;
        for (int i = 0; i < 32; i++) {
            if ((n & mask) != 0) {
            mask <<= 1;
        return count;

From what we’ve entered, the 32-bit binary number is out of sight, so we cycle 32 times, each time a judgment (N & Mask). According to the nature and nature of the calculation, if there is 0 the result is 0. The mask is always 1, so only when n == 1 is set when n == 1. Therefore, both methods have the same operation and judge whether a given paragraph is 1.

  • Question 2 Hanming distance

The distance between two integers is the number of two digits corresponding to different binary positions.
Give two integers x and y to calculate the distance between them.

Ввод: x = 1, y = 4

 Вывод: 2

1   (0 0 0 1)
4   (0 1 0 0)
       ↑   ↑

 Стрелка выше указывает на различные позиции, соответствующие бинарной позиции.

I use the most primitive way to solve this question, because according to the value meaning, let’s find the binary numbers of two integers, so we can solve it according to the binary integer method.

class Solution {
    public int hammingDistance(int x, int y) {
        int count = 0;
        while(x!=0 || y!=0){
            if(x%2 != y%2){
        return count;

equals to determine whether two binary numbers are equal, so we first divide by 2, then take the remainder, and then determine whether two numbers are equal. The last condition is that the two integers are 0 and finally 0.

  • Third question – inverted binary position

Reverse the binary position of a 32-bit integer.

Вход: 0000001010010100000111010011100
 Выход: 0011001011110000010100101000000
 Объяснение: Входная двоичная строка 00000010100101000001110100111100 указывает на несимболическое целое число 43261596,
           Таким образом, 964176192 возвращается, и его двоичная форма представления - 00110010111100000101001010000.

This question is very similar to the “reflection of integers” problem. It is only one decimal and one binary.
Integer integer topics link:
Basic code to invert an integer:y = y * 10 + x % 10; x = x / 10;
We can then have the inspired use operations, the 32-bit operations, and then the ANS responsibility to add and get the final result, as well as the left or augmented bit operation.

public class Solution {
    public int reverseBits(int n) {
       int ans = 0;
       for(int i=0;i<32;i++){
           ans <<= 1;
           ans+=(1 & n);
       return ans;

The key to this question is to get the last N, how do you get it? Using the (1 and N) operation, 0 or 1 can be obtained according to the end-end or an odd number. After we get it, let’s open the ANS on the left plus the value of that end, then the value of the end n will appear. , Then we take the second one, so we use itn>>=1Come and remove the latter, pay the former penalty from the first position to the first position, and rotate 32 times alternately. ANS final two is fine.

  • Question 4 Yang Hui Triangle

Given a negative integer and constructing the previous Yang Hui Triangle Numbrows series.

Вход: 5

Yang Hui’s triangle is not a process question, but I think this topic is very interesting and general, so I took it up and put it here just to talk about the implementation process.

First of all, we can know that this is a typical example that requires a nested loop. Each line is the first and the tail of the inner loop, and from the second position is the J-in the Line (i-1). One amount of harmony with J until we get the final result.
main code:res.add(ret.get(i-1).get(j-1)+ret.get(i-1).get(j));

class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> ret = new ArrayList<List<Integer>>();
        for(int i=0;i<numRows;i++){
            List<Integer> res = new ArrayList<>();
            for(int j=0;j<=i;j++){
                if(j==0 || j==i){
        return ret;

Well, today’s exchange will be here first. I hope the students will be happy to beat the questions and the AC questions. Then I hope my algorithm competition can do well, come on!
Only sauce ~

Leave a Comment