我怎样才能得到位的位置 [英] How can I get the position of bits

查看:131
本文介绍了我怎样才能得到位的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个十进制数,我需要将其转换为二进制数,然后在该二进制表示中找到一个的位置。

I have a decimal number which I need to convert to binary and then find the position of one's in that binary representation.

输入为5,其二进制数为 101 ,输出应为

Input is 5 whose binary is 101 and Output should be

1
3

以下是我的代码,它只提供输出 2 而不是我想要以二进制表示形式提供一个人的位置。如何从1开始获取设置位的位置?

Below is my code which only provides output as 2 instead I want to provide the position of one's in binary representation. How can I also get position of set bits starting from 1?

public static void main(String args[]) throws Exception {
    System.out.println(countBits(5));
}

private static int countBits(int number) {
    boolean flag = false;

    if (number < 0) {
        flag = true;
        number = ~number;
    }
    int result = 0;
    while (number != 0) {
        result += number & 1;
        number = number >> 1;
    }
    return flag ? (32 - result) : result;
}


推荐答案

你想要 countBits 返回结果,而不是在方法中放置 System.out.println ,这通常是最好的方法。如果你想让它返回一个位位列表,那么模拟就是让你的方法返回一个数组或某种List,比如:

Your idea of having countBits return the result, instead of putting a System.out.println inside the method, is generally the best approach. If you want it to return a list of bit positions, the analogue would be to have your method return an array or some kind of List, like:

private static List<Integer> bitPositions(int number) {

正如我在评论中提到的,你会让生活变得更轻松如果您使用>>> ,请自行删除特殊代码以检查否定关键字。这样做,并调整你已经拥有的代码,给你类似

As I mentioned in my comments, you will make life a lot easier for yourself if you use >>> and get rid of the special code to check for negatives. Doing this, and adapting the code you already have, gives you something like

private static List<Integer> bitPositions(int number) {
    List<Integer> positions = new ArrayList<>();
    int position = 1;
    while (number != 0) {
        if (number & 1 != 0) {
            positions.add(position);
        }
        position++;
        number = number >>> 1;
    }
    return positions;
}

现在,来电者可以按照自己的意愿打印出头寸。如果你使用 System.out.println ,输出将是 [1,3] 。如果您希望每个输出单独一行:

Now the caller can do what it wants to print the positions out. If you use System.out.println on it, the output will be [1, 3]. If you want each output on a separate line:

for (Integer position : bitPositions(5)) {
     System.out.println(position);
}

无论如何,关于如何打印头寸的决定(或其他任何事情)你想要用它们)与计算位置的逻辑分开,因为该方法返回整个列表并且没有自己的 println

In any case, the decision about how to print the positions (or whatever else you want to do with them) is kept separate from the logic that computes the positions, because the method returns the whole list and doesn't have its own println.

(顺便说一句,正如亚历克斯所说,最常见的是将低位比特视为位0而不是位1,尽管我见过硬件调用低位bit 31和高位bit 0的手册。将其称为bit 0的优点是位置N的1位代表值2 N ,简单化。我的代码示例在您的问题中按照您的要求将其称为位1;但如果您想将其更改为0,只需更改位置的初始值。)

(By the way, as Alex said, it's most common to think of the lower-order bit as "bit 0" instead of "bit 1", although I've seen hardware manuals that call the low-order bit "bit 31" and the high-order bit "bit 0". The advantage of calling it "bit 0" is that a 1 bit in position N represents the value 2N, making things simple. My code example calls it "bit 1" as you requested in your question; but if you want to change it to 0, just change the initial value of position.)

这篇关于我怎样才能得到位的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆