public void test() {
String s = "Hello";
StringBuilder t = new StringBuilder(s.length());
for (int i = 0; i < s.length(); i++) {
t.append((char) inc(s.charAt(i)));
}
System.out.println(s);
System.out.println(t.toString());
}
private int inc(int x) {
// Check each bit
for (int i = 0; i < Integer.SIZE; i++) {
// Examine that bit
int bit = 1 << i;
// If it is zero
if ((x & bit) == 0) {
// Set it to 1
x |= bit;
// And stop the loop - we have added one.
break;
} else {
// Clear it.
x &= ~bit;
}
}
return x;
}
4条答案
按热度按时间8yparm6h1#
只需加1(字符可视为int16):
n3schb8v2#
仅递增1。
v7pvogib3#
非常简单,只需要将char转换为int型。
然后你需要在
ascii
中加1并将其转换回来。
2j4z5cfb4#
这里有一个方法,它只使用按位函数将参数加1。
印刷品