import java.io.*;

class BinaryCounter{
        private String b;
        private int binary;
        public String binaryConverter(int binary){

                this.binary = binary;
                String  temp;
                temp = Integer.toBinaryString(binary);
                return temp;
        }
        public String  INCR(String b){
                this.b = b;
                char[] temp1  = new char[b.length()];
                temp1 = b.toCharArray();
                char[] temp2  = new char[temp1.length+1];


                for(int h = 0 ; h < b.length() ; h++) temp2[h+1] = temp1[h];
                int i = b.length();

                while(temp2[i]=='1'){

                        temp2[i] = '0';
                        i--;
                }

                temp2[i] = '1';

                return new String(temp2,1,b.length());

        }
        public static void main(String args[])throws Exception{
                BinaryCounter b1 = new BinaryCounter();
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("Enter any number of your choice");
                String b = br.readLine();
	int h = Integer.parseInt(b);

                String store = b1.binaryConverter(h);
                String store1 = null;
                for(int i = 0; i < 8 ; i++){
                        store1 = b1.INCR(store);
                        System.out.println("Incremented Number:"+store1);
                        store = store1;
                }


        }


}

***************OUTPUT**********************

C:\Users\dell\java>javac BinaryCounter.ja

C:\Users\dell\java>java BinaryCounter
Enter any number of your choice
12
Incremented Number:1101
Incremented Number:1110
Incremented Number:1111
Incremented Number:0000
Incremented Number:0001
Incremented Number:0010
Incremented Number:0011
Incremented Number:0100

C:\Users\dell\java>java BinaryCounter
Enter any number of your choice
100
Incremented Number:1100101
Incremented Number:1100110
Incremented Number:1100111
Incremented Number:1101000
Incremented Number:1101001
Incremented Number:1101010
Incremented Number:1101011
Incremented Number:1101100

Edited 5 Years Ago by DarkPheonix: n/a

One thing you could do is explain what the printed output means?

Incremented Number:1111
Incremented Number:0000 <<<<<<<<<< should this be 10000

Edited 5 Years Ago by NormR1: n/a

One thing you could do is explain what the printed output means?

well the output is the incremented binary values of the input...
the number of output is as specifed in the for loop ... that is 8 times
the input in the first was 12
and in second case was 100

the reason im asking for help is that there is the use of the two char arrays temp1 and temp2.
in order to avoid the ArrayIndexOutOfboundsException and to facilitate the carry over that we usually use in boolean alzebra ... that is 1+1 = 10 .... here 1 hasbeen carried over...
so i thought of increasing the size of the array temp2 by temp1.length+1
temp1 stores the characters of the string b;
but the thing is im wasting too much memory using this techniqu.... could u please suggest somthing to make it more efficient

well the output is the incremented binary values of the input...
the number of output is as specifed in the for loop ... that is 8 times
the input in the first was 12
and in second case was 100

the reason im asking for help is that there is the use of the two char arrays temp1 and temp2.
in order to avoid the ArrayIndexOutOfboundsException and to facilitate the carry over that we usually use in boolean alzebra ... that is 1+1 = 10 .... here 1 hasbeen carried over...
so i thought of increasing the size of the array temp2 by temp1.length+1
temp1 stores the characters of the string b;
but the thing is im wasting too much memory using this techniqu.... could u please suggest somthing to make it more efficient

Actually the program is supposed reset to 0000 after reaching the higest possible 4-bit number 1111.... it works as a counter ....
and this is also an assignment i got in colg (to introduce us to making faster algorithms)..... so im just doing wat the assignment asked me to do

The binaryConverter method doesn't nothing more than the Integer.toBinaryString()
method. There is no use for it.

im wasting too much memory using this technique

What technique? Adding ONE byte is too much?

Edited 5 Years Ago by NormR1: n/a

Most people have absolutely no idea just how much optimisation is applied to code after it enters the compiler and before the actual hardware memory locations in the runtime system get updated, so trying to double-guess the impact of a small code change is probably futile (but the answer will 99/100 be "less impact than you expect"). They can also be unrealistic about how long things (especially in Java) really take. Have you been able to benchmark your code so you can assess your changes? My guess is that without the print statements it runs so fast that you have no meaningful way to instrument it ( < 1/60 sec). Similarly for memory usage - it it's less than MBytes then it's irrelevant, and your arrays with, gasp! gulp! maybe even ten bytes in them - you must be joking.
What you can do is to evaluate your algorithm in terms of how it scales with size. Obviously an algorithm O(n) is going to be "better" than one that's O (e^n), but for that you don't need to execute the code.
To tune actual code, assuming it implements the best algorithm, you first have to find a test problem that takes (eg) a few seconds to run, then use that as the baseline.

The binaryConverter method doesn't nothing more than the Integer.toBinaryString()
method. There is no use for it.


What technique? Adding ONE byte is too much?

no ... the problem is wen that byte is the most significant ..... on using a single array its goin outofbounds ....so i have to reallocate it memory every time i recall it in the main.... temp2[i+1] = temp1... so an extra unnecessary element is being added..... can u tell me how to remove this problem

Most people have absolutely no idea just how much optimisation is applied to code after it enters the compiler and before the actual hardware memory locations in the runtime system get updated, so trying to double-guess the impact of a small code change is probably futile (but the answer will 99/100 be "less impact than you expect"). They can also be unrealistic about how long things (especially in Java) really take. Have you been able to benchmark your code so you can assess your changes? My guess is that without the print statements it runs so fast that you have no meaningful way to instrument it ( < 1/60 sec). Similarly for memory usage - it it's less than MBytes then it's irrelevant, and your arrays with, gasp! gulp! maybe even ten bytes in them - you must be joking.
What you can do is to evaluate your algorithm in terms of how it scales with size. Obviously an algorithm O(n) is going to be "better" than one that's O (e^n), but for that you don't need to execute the code.
To tune actual code, assuming it implements the best algorithm, you first have to find a test problem that takes (eg) a few seconds to run, then use that as the baseline.

yeah i just learnt that a few days ago ... :P ...
but a frend of mine keeps sayin "memory" management is improtant n stuff... so .... thats y i asked....

This article has been dead for over six months. Start a new discussion instead.