I need to convert a large decimal to binary how would I go about doing this? Decimal in question is this 3324679375210329505
相关问题
- 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
I would use a Stack! Check if your decimal number is even or odd, if even push a 0 to the stack and if its odd push a 1 to the stack. Then once your decimal number hits 1, you can pop each value from the stack and print each one.
Here is a very inefficient block of code for reference. You will probably have to use long instead of integer.
http://www.wikihow.com/Convert-from-Decimal-to-Binary
How about:
If you want something fast (over 50% faster than
Long.toString(n, 2)
and 150-400% faster thanBigInteger.toString(2)
) that handles negative numbers the same as the built-ins, try the following:If you want the actual Two's Compliment binary representation of the
long
(with leading 1s or 0s):You may want to go for
BigDecimal
.A bit pointless, but here is a solution in C:
For the example, it prints out:
EDIT: And if you don't mind leading zeros....