Last active
August 29, 2015 14:09
-
-
Save Deathnerd/9f3dd3b5d5173f2af8bd to your computer and use it in GitHub Desktop.
Prints out all binary values from 0 to 2^n. BinaryStrings is my method, and FastBinaryStrings (hah) is the method I found on Stack Overflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To change this license header, choose License Headers in Project Properties. | |
| * To change this template file, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| /** | |
| * Class: CSC 190 | |
| * Date: 11/22/2013 | |
| * Project: | |
| * @author Wes Gilleland | |
| * Purpose: | |
| */ | |
| import org.apache.commons.lang.time.StopWatch; | |
| class BinaryStrings { | |
| /** | |
| * Prints every binary value from 0 to 2^n | |
| * | |
| * @param n The n of 2^n | |
| */ | |
| public static void printBinary(int n) { | |
| long number = 1; | |
| String s = ""; | |
| /* | |
| * Prep the binary string to have n number of zeroes and find 2^n | |
| */ | |
| for (int i = 0; i < n; i++) { | |
| s += "0"; | |
| number *= 2; | |
| } | |
| /*Print the zeroes*/ | |
| System.out.println(s); | |
| /*Print all the other binary numbers*/ | |
| for (int i = 0; i < number - 1; i++) { | |
| s = incrementBinary(s); | |
| System.out.println(s); | |
| } | |
| } | |
| /** | |
| * Takes in a string of binary and increments it by 1 | |
| * | |
| * @param binary The binary string to increment | |
| * @return The incremented binary string | |
| */ | |
| public static String incrementBinary(String binary) { | |
| int length = binary.length(); | |
| int currentPosition = length - 1; | |
| /* | |
| * While the current character is not at '0', keep replacing the current character with a '0' | |
| */ | |
| while (binary.charAt(currentPosition) != '0') { | |
| binary = replace(binary, currentPosition, "0"); | |
| currentPosition--; | |
| } | |
| return replace(binary, currentPosition, "1"); | |
| } | |
| /** | |
| * Replaces a character in a given string at a given position | |
| * | |
| * @param inputString The string to replace | |
| * @param insertPosition The position of the character to insert | |
| * @param characterToInsert The character to insert | |
| * @return The modified string | |
| */ | |
| private static String replace(String inputString, int insertPosition, String characterToInsert) { | |
| int length = inputString.length(); | |
| if (insertPosition == length) { | |
| return inputString.substring(length - 1) + characterToInsert; /*//replace the character at the end of the string*/ | |
| } else if (insertPosition == 0) { | |
| return characterToInsert + inputString.substring(1); /*//replace the character at the beginning of the string*/ | |
| } | |
| return inputString.substring(0, insertPosition) + characterToInsert + inputString.substring(insertPosition + 1, length); /*// replace the character in the middle of the string*/ | |
| } | |
| } | |
| class FastBinaryStrings { | |
| public static void doStuff(long num1, long num2) { | |
| for (long i = 0; i < num2; i++) { | |
| String result = ""; | |
| for (long j = num1; j > 0; j >>= 1) { | |
| if ((j & i) != 0) { | |
| result += "1"; | |
| } else { | |
| result += "0"; | |
| } | |
| } | |
| System.out.println(result); | |
| } | |
| } | |
| } | |
| public class lab14 { | |
| public static void main(String[] args) { | |
| StopWatch stopwatch = new StopWatch(); | |
| /*Scanner in = new Scanner(System.in); | |
| BinaryStrings myBS = new BinaryStrings(); | |
| System.out.print("Enter a positive integer: "); | |
| myBS.printBinary(in.nextInt());*/ | |
| int n = 22; | |
| long num1 = (long) Math.pow(2, n); | |
| long num2 = num1 * 2; | |
| long time1; | |
| long time2; | |
| String format = "Time for %s to do %d bits: %10.3f seconds%n"; | |
| stopwatch.start(); | |
| FastBinaryStrings.doStuff(num1, num2); | |
| stopwatch.stop(); | |
| time1 = stopwatch.getTime(); | |
| stopwatch.reset(); | |
| stopwatch.start(); | |
| BinaryStrings.printBinary(n); | |
| stopwatch.stop(); | |
| time2 = stopwatch.getTime(); | |
| System.out.printf(format, "FastBinaryStrings", n, (float) time1 / 1000); | |
| System.out.printf(format, "BinaryStrings", n, (float) time2 / 1000); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment