#convert negatif number in binary with a fixed number of bit

60 messages · Page 1 of 1 (latest)

storm pumiceBOT
#

This post has been reserved for your question.

Hey @blazing flame! Please use /close or the Close Post button 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.

blazing flame
#

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

blazing flame
#

I tried this

#

but it doesnt work because i have way too much number

waxen gull
blazing flame
#

do you mean an other version of the method Integer.toBinaryString ?

waxen gull
blazing flame
#

because im doing smth that need 11 number no matter how bit fits

#

otherwise i wouldnt be there

waxen gull
blazing flame
#

its impossible

#

even if it would be the case its a project and i wont be facing this situation

waxen gull
blazing flame
#

it wont happen

waxen gull
#

so you are only concerned with padding

blazing flame
#

yep

waxen gull
# blazing flame yep

its basically the same what you already have

        int number = 1;
        String s = String.format("%11s",Integer.toBinaryString(number)).replace(' ','0');
blazing flame
#

doesnt work

#

already tried

waxen gull
#

it does work for me

blazing flame
waxen gull
#

you just told me that there is no way a string can be longer than 11 chars.

blazing flame
#

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

waxen gull
#

thats just how binary numbers are represented

blazing flame
#

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

wooden delta
blazing flame
#

?

wooden delta
blazing flame
#

0000 0000 010

#

but why 14 though?

wooden delta
#

Because 14 looks like 0000 0000 1110

#

Or 0000 0001 110

wooden delta
#

Can you share the question?

blazing flame
#

in my code the binary string always start with a 1

wooden delta
wooden delta
blazing flame
#

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

wooden delta
#

That doesn't make sense sorry

blazing flame
#

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

wooden delta
#

What is b#

blazing flame
#

its like instruction but for a processor