How to convert a long to a fixed-length 16-bit binary string?

前端 未结 8 1035
谎友^
谎友^ 2020-12-10 17:23

Hi i want to convert a long integer to binary but the problem is i want a fixed 16 bit binary result after conversion like if i convert 2 to 16 bit binary it should give me

8条回答
  •  情深已故
    2020-12-10 17:41

    Integer.toBinaryString will convert an int to its binary representation as a string.

    It does not give you leading zeroes, so if you really need the string to have those and be 16 bits, you can just add them yourself.

    You should know how to do that.


    Do note that an int is actually 32 bits in Java. You should also know how two's complement works. The binary representation of -1, for example, is 32 1s.

提交回复
热议问题