Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Binary Counter / Flipper

 
Miling Shah
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My task is to print an n-bit(n is accepted as input from user) binary sequence as :
For n=4,
Output:
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111

I found three ways to do this:
Algo 1. Run a loop for i=0 to 2^(n), step i by 1
print(Dec2Bin(i,n));

where Dec2Bin is a function that converts a decimal i into a String of size n (if possible without data value damage/loss )

Code for this algorithm:


Algo 2. Create a bit array
Check bits from right (LSB) to left (MSB).
if bitvalue @ index == 0, flip bit @ index to 1
else if bitvalue @ index == 1, recursively check for left bits,
as terminal case.. if bit @ index=0 is 0 n all bits to the right are 1
flip bit at index=0 to 1 and flip all the bits to the right to 0

I tried implementing this algo, but it didnt work well,, it gives me the same output for n>4, as that given by n=4.
I would be glad if someone could help me with debugging/fixing this piece of code.
My Code :


Algo 3. I just thought that maybe every bit 0 or 1 can be calculated dynamically and directly displayed..
using the Bin2Dec single bit conversion in simpler steps as shown in this code snippet, but unfortunately even this doesnt work:


Any help is appreciated... I want to fix the code, atleast there is a silly logical error in Algo 3.
Algo 2 seems a little too weird.. But still i guess it does work if coded well.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
substring and Integer are your friends ... ;)



WP

Can you figure out how this works?
FYI: Not assuming this will work for all cases!! Negative input etc..

 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What was the problem with Algo 1. Out of the 3, Algo 1 seems the simplest.
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought you can reduce those algorithms to about ten lines, including lines with only a { on. I would use a StringBuilder rather than toBinaryString. The + operator for String catenation is possible, but less efficient if used in several statements.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: The + operator for String catenation is possible, but less efficient if used in several statements.


True, but lets get real, for any actual use of this code, performance is not relevant.
Even if one approach is ten times slower, no one will ever notice.
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Miling Shah wrote: . . . where Dec2Bin is a function that converts a decimal i into a String of size n . . . .
Not in the example shown; it returns an int[].
A decimal to binary conversion is really simple. Look:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic