Skip to content

Instantly share code, notes, and snippets.

@deyindra
Created February 1, 2016 03:23
Show Gist options
  • Save deyindra/6fa6cb94c26b45b2b62c to your computer and use it in GitHub Desktop.
Save deyindra/6fa6cb94c26b45b2b62c to your computer and use it in GitHub Desktop.
Binary String Generator for 32 Bit positive Integer
import java.util.Iterator;
import java.util.NoSuchElementException;
public class BinaryNumberGeneratorIterator implements Iterator<String> {
private final int size;
private final int bit;
private int currentBitPostion;
public BinaryNumberGeneratorIterator(int bit) {
assert(bit<=31);
this.bit = bit;
if(bit<31)
size =1<<bit;
else
size=Integer.MAX_VALUE;
currentBitPostion=0;
}
@Override
public boolean hasNext() {
return currentBitPostion<size;
}
@Override
public String next() {
if(!hasNext()){
throw new NoSuchElementException();
}
StringBuilder sb = new StringBuilder();
for(int i=bit-1;i>=0;i--){
if((currentBitPostion & (1<<i))!=0){
sb.append("1");
}else{
sb.append("0");
}
}
currentBitPostion++;
return sb.toString();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
public static void main(String[] args) {
BinaryNumberGeneratorIterator iterator = new BinaryNumberGeneratorIterator(31);
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment