简体   繁体   English

在Java中将int转换为十六进制

[英]Convert an int to hex in Java

I need to find out if int n is a power of two and my approach is to convert n to a hex number and check each bit (0 or 1). 我需要确定int n是否为2的幂,并且我的方法是将n转换为十六进制数并检查每个位(0或1)。 However, I've never used hex numbers in Java, could anyone help me out here? 但是,我从未在Java中使用过十六进制数字,有人可以在这里帮助我吗?

Both converting to a String and replacing using a regular expression is expensive. 转换为String以及使用正则表达式进行替换都非常昂贵。

A simple way to check for a (positive) power of two is to check the number bits set. 检查(正)2的幂的简单方法是检查设置的位数。

if (x > 0 && Long.bitCount(x) == 1)

While Long.bitCount looks complicated, the JVM can replace it with a single machine code instruction. 虽然Long.bitCount看起来很复杂,但JVM可以用一条机器代码指令替换它。

The canonical way (which you will encounter a lot when googling how to test for powers of two, and you will probably encounter it in code) to test for powers of two is 测试2的幂的规范方法(在谷歌搜索如何测试2的幂时会遇到很多,并且可能在代码中会遇到)

x != 0 && (x & x - 1) == 0

It is often encountered with more parenthesis (precedence paranoia?) 经常遇到带有更多括号的情况(优先偏执狂?)

x != 0 && (x & (x - 1)) == 0

It is common to skip the x != 0 check and guarantee zero can't be an input, or (often enough) zero is "as good as" a power of two in some sense. 通常会跳过x != 0检查并确保零不能输入,或者(通常)零在某种意义上“等于” 2的幂。

You can of course change the condition to x > 0 if you don't want to consider -2147483648 a power of two, though in many cases it would be (because it's congruent to 2 31 modulo 2 32 , so interpreted unsigned it's a PoT, and anyway it only has one bit set so it was a PoT all along) 当然,您可以更改条件x > 0 ,如果你不想考虑-2147483648二的幂,尽管在许多情况下,这将是(因为它是全等到2 31模2 32,这样解释的签名是一个锅,而且总的来说只有一点点设置,所以一直都是PoT)


So what's the deal with x & x - 1 . 那么x & x - 1有什么用呢? It unsets the lowest set bit, but let's look at it in the context of PoTs. 它取消设置最低位,但让我们在PoT的背景下进行查看。

Let's ignore x<2 and say x has the form a10 k (ie an arbitrary string of bits followed by a one followed by k zeroes), subtracting 1 from it gives a01 k because the -1 borrows through all the trailing zeroes until it reaches the lowest set bit, unsets that bit, then the borrow dies and the top bits are unchanged. 让我们忽略x <2并说x的形式为a10 k (即,任意位串后跟一个1,后跟k个零),从中减去1得到a01 k,因为-1借用了所有尾随的零直到达到最低的置位,不置位,然后借位死,高位不变。

If you take the bitwise AND of a10 k and a01 k you get a00 k because something ANDed with itself is itself again (the a ), and in the tail there's always a 0 involved in the AND so it's all zeroes. 如果对a10 k和a01 k进行按位AND,则得到a00 k,因为与自身进行与运算的对象本身又是自身( a ),并且在AND的尾部始终有一个0,因此全为零。

Now there are two cases. 现在有两种情况。 0) a=0. 0)a = 0。 Then the result is zero, and we know we started out with x of the form 10 k which is a power of two. 然后结果为零,我们知道我们以10 k形式的x开始,它是2的幂。 And 1) a!=0, then the result isn't zero either because a still appears in it, and x wasn't a power of two because it has at least two set bits (the lowest set bit which we explicitly looked at, and at least one other somewhere in a ). 并且1)a!= 0,则结​​果也不为零,这是因为a仍然出现a x,并且x不是2的幂,因为x至少具有两个设置位(我们明确查看的最低设置位) ,和至少一个在其他某处a )。

Well actually there are two more cases, x=0 and x=1 which were ignored. 那么实际上还有另外两种情况,x = 0和x = 1被忽略了。 If we allow k to be zero then x=1 is also included. 如果我们允许k为零,则x = 1也包括在内。 x=0 is annoying though, in x & x - 1 there is that x as the left operand of & , so the result must be zero no matter what happens in the right operand. 但是x = 0很烦人,在x & x - 1中, x&的左操作数,因此无论右操作数发生什么,结果都必须为零。 So it falls out as a special case. 因此,它属于特殊情况。

There is no reason to convert to hex to check bits. 没有理由转换为十六进制来检查位。 In fact, that actually makes it harder. 实际上,这实际上使它变得更加困难。 The following line produces a binary String for an int variable "num": 以下行为int变量“ num”生成二进制字符串:

String binary = Integer.toString(num, 2)

That String will only have 1s and 0s in it. 该字符串将只有1和0。 Then eliminate any 0s: 然后消除任何0:

String ones = binary.replace("0", "");

If the length of the String is more than one, there there is more than one bit, which means it is not a power of 2. If the length is 0, it means the number had no bits, which means it is 0. 如果String的长度大于1,则存在一个以上的位,这表示它不是2的幂。如果长度为0,则表示该数字没有位,即为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM