Kotlin将数字分解为2的幂

时间:2019-08-15 17:57:16

标签: kotlin bit-shift

嗨,我正在用Kotlin编写应用程序,需要将数字分解为2的幂。

我已经在c#,PHP和swift中完成了此操作,但是kotlin的工作方式有所不同。

对此进行了研究,我认为这与代码中的数字在某些地方变为负数有关,解决方案在于将一个或多个变量声明为“ Long”以防止这种情况的发生,但是我无法弄清楚如何做到这一点。

这是我的代码:

    var salads = StringBuilder()
    var value = 127
    var j=0
    while (j < 256) {

        var mask = 1 shl j
        if(value != 0 && mask != 0)  {

            salads.append(mask)
            salads.append(",")

        }
        j += 1
    }

    // salads = (salads.dropLast()) // removes the final ","
    println("Salads = $salads")

此应输出以下内容:     1,2,4,8,16,32,64 我真正得到的是:

1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,-2147483648,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,-2147483648,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,-2147483648,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,-2147483648,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,-2147483648,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,-2147483648,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,-2147483648,1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144,524288,1048576,2097152,4194304,8388608,16777216,33554432,67108864,134217728,268435456,536870912,1073741824,-2147483648,

有什么想法吗?

4 个答案:

答案 0 :(得分:0)

这至少适用于您指定的一个输入:

fun powersOfTwo(value :Long): String {

    val result = ArrayList<String>()

    var i = 0
    var lastMask = 0

    while (lastMask < value) {

        val mask = 1 shl i

        if (value != 0.toLong() && mask < value) {

            result.add(mask.toString())
        }

        lastMask = mask
        i += 1
    }

    return result.joinToString(",")
}

在单元测试中运行它:

@Test
fun addition_isCorrect() {

    val result = powersOfTwo(127)

    assertEquals("1,2,4,8,16,32,64", result)
}

测试通过。

答案 1 :(得分:0)

嗨,我终于设法使它正常工作:

fun decomposeByTwo(value :Int): String {

val result = ArrayList<String>()
var value = value
var j = 0
while (j < 256) {

    var mask = 1 shl j
    if ((value and mask) != 0) {
        value -= mask
        result.add(mask.toString())

    }
    j += 1
}

return result.toString()
}

我希望这可以帮助尝试使用按位期权的人!

答案 2 :(得分:0)

您可以获取Int中适合的所有2的幂的列表,并使用中缀函数and测试每个值是否包含它:

val value = 126

val powersOfTwo = (0 until Int.SIZE_BITS).map { n -> 1 shl n }
println(powersOfTwo.filter { p -> value and p != 0}.joinToString(","))
// prints: 2,4,8,16,32,64

在Kotlin游乐场中查看完整的代码:https://pl.kotl.in/f4CZtmCyI

答案 3 :(得分:-1)

您想以某种方式对“值”和“掩码”进行“按位与”以确定是否设置了“值”的第j位。我认为您只是忘记了Kotlin实施中的测试。