• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I generate this code in reverse order the code is generating binary of any number  RSS feed

 
Ibrahim Arowolo
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is calculating the binary of any number but it's giving an error of returning as it's been stored after the modulus calculation instead of generating from the last number.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ibrahim Arowolo wrote:The code is calculating the binary of any number but it's giving an error of returning as it's been stored after the modulus calculation instead of generating from the last number.

I'm afraid I've no idea what that means. Can you give us an example of the input you are using and what output you were expecting and what output you actually got.
And can you also post the whole method, properly indented. UseCodeTags - it makes it a lot easier to read.
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ibrahim Arowolo wrote:while (k != 0)

I don't understand the problem that you are describing, but it seems like you would want an if rather than a while to properly populate the array with both 1's and 0's.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote: . . . UseCodeTags - it makes it a lot easier to read.
Since he's new, I'll add code tags, which make the code a lot better, but still highlight inconsistent indenting. I also removed a blank line because too many blanks make the code harder to read.

What does f mean? Do you actually need it, since you know you are going to get 32 digits from an int?

And welcome to the Ranch
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ibrahim Arowolo wrote:The code is calculating the binary of any number

Actually, it isn't, but more on that later.

but it's giving an error of returning as it's been stored after the modulus calculation instead of generating from the last number.

What error? I suspect you mean that it's not doing what you expect, which is not the same thing.

Look at what you've written and translate it into English (or your native language):
For each "binary digit", you're executing a while loop which does the correct calculation, but you're then putting the result into the same digit. Is that what you want?

And if not, what do you think you need to do to make it work?

Now, back to your "any number" claim. Once you've got it working, test your program when k is negative. I suspect you won't see what you were expecting, and the explanation can be found here.

HIH

Winston
 
Jitender Raghuvanshi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, all i can understand is that you want to print a binary number of whatever you pass into the method.
Even i am a beginner in java and in love with Java Collections.
So i just used Collections to solve this problem. I hope it helps.
And i actually don't know how to post codes here. So just copy pasting.


 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

We usually discourage people from posting the whole solution before the OP has a good chance to work it out, and delete posts or parts of posts which contain entire solutions, but I have decided to leave that code so people can see why it will not work properly.
 
Jitender Raghuvanshi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

We usually discourage people from posting the whole solution before the OP has a good chance to work it out, and delete posts or parts of posts which contain entire solutions, but I have decided to leave that code so people can see why it will not work properly.


and you wont explain what is wrong with the code?
That's rude! :P
Anyway, its working fine at my end.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I may have seen an error which is not there. Sorry. But have you tried it with -123?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jitender Raghuvanshi wrote:and you wont explain what is wrong with the code?
That's rude! :P

As rude as not reading our HowToAnswerQuestionsOnJavaRanch (←click) page before posting an answer?

Because if you had, you'd have seen our version of the Hippocratic oath: FirstRuleDoNoHarm (the first link).
And also: DontBeACodeMill.

However, I'm sure your intentions were great, and welcome to the Ranch, Jitender.

Anyway, its working fine at my end.

Except, as Campbell said (and I mentioned earlier), that code will NOT give you what you expect with negative numbers.

Winston
 
Jitender Raghuvanshi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I may have seen an error which is not there. Sorry. But have you tried it with -123?


Hey! Thanks for the correction.

Try it now...

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That might print negative numbers right, but I think it is no better as a solution, I am afraid.
 
Jitender Raghuvanshi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That might print negative numbers right, but I think it is no better as a solution, I am afraid.

how about using this method ->>Integer.toBinaryString();
Would it help??
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jitender Raghuvanshi wrote: . . . how about using this method ->>Integer.toBinaryString();
Would it help??
No.

Such exercises are supposed to be solved without using toBinaryString or similar methods.
You can print out a number's binary digits backwards with a for loop containing a single line. Like thisIf printing forwards, the code will be easier to read if you use two lines inside the loop. Repeated use of the / operator will reduce most negative numbers to 0, so you may need to use a bitwise operator to get the 1111111 at the left end of the output. You can read about bitwise operators in this thread.
I would like to see this sort of output, with 32 characters per number, although it wraps awkwardly on a terminal with 80 line width.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!