Skip to content

Instantly share code, notes, and snippets.

@kinow
Created September 1, 2016 04:22
Show Gist options
  • Save kinow/0edb3bebfe0d8e3386dd83481dc48b76 to your computer and use it in GitHub Desktop.
Save kinow/0edb3bebfe0d8e3386dd83481dc48b76 to your computer and use it in GitHub Desktop.
public static void main(String[] args) {
int accumulator = 0;
byte b = 0;
int nbBitsUsed = 100;
/* classic version */
while (nbBitsUsed >= 8) { /* each while test is a branch */
accumulator <<= 8;
accumulator += b++;
nbBitsUsed -= 8;
}
System.out.println(accumulator);
System.out.println(b);
System.out.println(nbBitsUsed);
System.out.println();
accumulator = 0;
b = 0;
nbBitsUsed = 100;
int nbBytesUsed = nbBitsUsed >> 3;
int ptr = 0;
nbBitsUsed &= 7;
ptr += nbBytesUsed;
accumulator = ptr;
System.out.println(accumulator);
System.out.println(b);
System.out.println(nbBitsUsed);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment