如何按位移位Java中的二进制字符串? [英] How to bitwise shift a binary string in java?
问题描述
我有一个10位二进制字符串,我必须逐位在每次迭代循环移位。我很迷茫,失去了做这件事。还有什么比这背后的逻辑是很容易做的?如果是十六进制数,我们可以通过NUM做到>> 1或num<< 1,但像1010101010的字符串应转换为十六进制我们采用按位移位之前。我要申请按位转移循环10次。
I have a 10 bit binary string and i have to bitwise shift circularly at each iteration. I am so confused and lost doing it. What could be the logic behind it to do easily? If it is a hex number, we can do it by num>>1 or num<<1 but string like "1010101010" should be converted to hex before we apply bitwise shift. I have to apply the bitwise shift circularly 10 times.
推荐答案
有在Integer类方法转换到/从二进制字符串。
There are methods in the Integer class to convert to/from Binary Strings.
int i = Integer.parseInt("1010101010", 2);
String shiftedi = Integer.toBinaryString(i<<1);
编辑,上面是不是圆的,虽然,但你可以用简单的字符串操作:
Edit, above is not circular though, but you can use simple Strings operations:
String in = "1010101010";
String shifted1 = in.substring(1)+in.substring(0,1);
这篇关于如何按位移位Java中的二进制字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!