#convert negatif number in binary with a fixed number of bit
60 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @blazing flame! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
I already have this code ton convert a number in binary with a fixed number of bit but idk how to do the other one
you know that there is a method for that Integer.toBinaryString(int i)
do you mean an other version of the method Integer.toBinaryString ?
no that is the method I just don't know why you would want to truncate the resulting String because it is already adjusted to fit the number
because im doing smth that need 11 number no matter how bit fits
otherwise i wouldnt be there
so if an int needs more than 11 bits you want to cut them of ?
its impossible
even if it would be the case its a project and i wont be facing this situation
thats still my question
it wont happen
so you are only concerned with padding
yep
its basically the same what you already have
int number = 1;
String s = String.format("%11s",Integer.toBinaryString(number)).replace(' ','0');
it does work for me
you just told me that there is no way a string can be longer than 11 chars.
ok there is no way the number need more than 11 numbers to fit
but for some reason toBinaryString use idk like 20 numbers to represent -2
or -2 needs only like 3 bits
my friend tried and its the same problem
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
thats just how binary numbers are represented
man look -2 is only on 4 bit not 11
so what im expected to have is 1110 with 7 0 in front
like 0000 0001 110
What about 14
?
What would 14 look like
in my code the binary string always start with a 1
^
Yes that's how 2s complement works
i have this
i have the little b# -2
that i get in a string
I need to do a code that get assembler langage and translate it in binary
so i already have a string that start with "11100" and i need to add the last part
That doesn't make sense sorry
wait
i get things like that and i have to translate it in binary
so when i get when b #-2 i need to translate it in binary
it works on 16 bits
i already have the 5 first the only last bits i need are thoses 11
and they are -2 in binary with the complement 2 BUT I NEED 11 BITS EXACTLY
What is b#
its like instruction but for a processor