## Options pitbull ft stephen marley

29 comments### Best share trading platform uk

Join Stack Overflow to learn, share knowledge, and build your career. 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 as ans can anyone help me? Most likely what you want is Integer.

The idea here is to get the zero padding by putting a 1 in the 17th place of your value, and then use String. This works, of course, only when you are certain that the input is a bit number. I'm presuming that you want a String output of fixed length Here's what the code would look like:. In contrast to many suggestions here: The reason is that as the name suggests this will only work for integers.

And for negative numbers the bit representation will change the first bit indicates a negative number. The two numbers below represent the same number in short and int.

So if you want to represent the raw bits you have received this is the general application of your problem , this function will generate strange output. Java doesn't provide the implementation for short, so you will have to provide your own. Something like this size is the number of bits:. 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. 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 1 s. In terms of an algorithm to convert base10 numbers to binary, I personally think the following is pretty straightforward:.

Binary is a representation and not a format to convert an integer to. For example, if you have an integer:. The binary representation will be Java has only signed integers, so this link will be helpful. By posting your answer, you agree to the privacy policy and terms of service. Email Sign Up or sign in with Google. How to convert a long to a fixed-length bit binary string? Is it a String of 1s and 0s that you want as the output?

A short in Java is still signed, though. I saw 16 bits and was thinking short. Yes, that definitely won't work if you start with a negative short. Here's what the code would look like: I didn't compile or run it, so make sure no bug is there: As I understand things, not running the code is OK, as long as you prove it correct.

Changed the number above Hence you can not cast the short if you are interested in the bit. Something like this size is the number of bits: Those two aren't the same numbers. What are you talking about? You are right, I used the wrong bit representation. I will update the numbers. You don't need all that. You should know how to do that. In terms of an algorithm to convert base10 numbers to binary, I personally think the following is pretty straightforward: I had to do it for a 32 bit number and ended up with: Shekhar 2, 1 25 For example, if you have an integer: Sign up or log in Sign up using Google.

Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Stack Overflow works best with JavaScript enabled.