Bit Manipulation and Time Complexity
Bitwise operators, bit manipulation tricks, and Big-O complexity analysis.
Java bitwise operators: & (AND), | (OR), ^ (XOR), ~ (NOT); << and >> shift bits left and right. Common tricks: (n & 1) tests parity, x ^ 1 toggles the lowest bit, a mask like 0xFF extracts a byte, and (n & (n-1)) == 0 detects a power of two. Big-O measures runtime growth with input size: O(1) constant, O(log n) for binary search, O(n) for a linear scan, O(n log n) for efficient sorts and O(n^2) for nested loops.
Example
if((n & 1) == 0) System.out.println("even");
No comments yet.