How to get 0-padded binary representation of an integer in java?

前端 未结 17 2288
我在风中等你
我在风中等你 2020-11-22 08:23

for example, for 1, 2, 128, 256 the output can be (16 digits):

0000000000000001
0000000000000010
0000000010000000
0000000100000000
17条回答
  •  再見小時候
    2020-11-22 08:41

    You can use lib https://github.com/kssource/BitSequence. It accept a number and return bynary string, padded and/or grouped.

    String s = new BitSequence(2, 16).toBynaryString(ALIGN.RIGHT, GROUP.CONTINOUSLY));  
    return  
    0000000000000010  
    
    another examples:
    
    [10, -20, 30]->00001010 11101100 00011110
    i=-10->00000000000000000000000000001010
    bi=10->1010
    sh=10->00 0000 0000 1010
    l=10->00000001 010
    by=-10->1010
    i=-10->bc->11111111 11111111 11111111 11110110
    

提交回复
热议问题