문제

int g = 269;
System.out.println( Integer.toBinaryString(g));//100001101
g<<=10;
System.out.println( Integer.toBinaryString(g));//1000011010000000000

I know I can do it like this to add "0"'s after the number, but what if I want to add them before it?

g>>=1; won't work, obviously.

도움이 되었습니까?

해결책

It doesn't make sense to print leading zero for an integer, or it's binary representation. How many of them you would like to print? And how would compiler know that on it's own. However, you can always format your string to be padded with zero like this:

int g = 269;
String paddedWithZero = String.format("%32s", Integer.toBinaryString(g)).replace(' ', '0');
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top