I have a number and I want to print it in binary. I don't want to do it by writing an algorithm, Is there any built-in function for that in Java?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
check out this logic can convert a number to any base
OR
There are already good answers posted here for this question. But, this is the way I've tried myself (and might be the easiest logic based → modulo/divide/add):
Binary representation of given int x with left padded zeros:
Assuming you mean "built-in":
See Integer documentation.
(
Long
has a similar method,BigInteger
has an instance method where you can specify the radix.)Simply try it. If the scope is only printing the binary values of given integer value. It can be positive or negative.
input
5
Output
101
It works with signed and unsigned values used powerful bit manipulation and generates the first zeroes on the left.