byte[] data = {1, 2, 3, 4}; int together = 0; for (byte b : data) { together = (together << 8) | b; } System.out.println(together); //0x01020304 = 16909060
byte b = '3'; // or 51 or 0x33 for (int i = 0; i < 8; i++) { //prints: 00110011 int mask = 0x0080 >> i; int bit = b & mask; bit = bit >> (7 - i); System.out.print(bit); } System.out.println();
private int buffer = 0b11001100; //or could be in a byte private int bits = 8; public int nextBit() { int bit = (buffer >> 7) & 1; //grab left-most bit in buffer buffer <<= 1; //equivalent to: buffer = buffer << 1; bits--; //what to do when bits == 0? return bit; }
Given an int array A and an int value X, print all index pairs (i,j) such that A[i] + A[j] == X.
Is your solution optimal (ie, most efficient in terms of worst-case big-O)? Prove it.
for (int i = 0; i < a.length; i++) { for (int j = 0; j < a.length; j++) { if (a[i] + a[j] == x) { System.out.print("(" + i + "," + j + ") "); } } }
Proof by contradiction: Suppose A contains all 1s and X == 2. Even if we could somehow magically find all solution pairs in faster time, simply printing those solutions would require printing all possible (i,j) pairs. So, in this worst-case scenario, O(n^2) is the best we can do.
Given a string, titlecase all of the words in that string. (Titlecase = intialial capital letter and the rest lowercase.)