In high-performance embedded audio or signal processing, circular buffers often require their size to be a Power of Two (e.g., 32, 64, 128). This allows the expensive modulo operator (%) to be replaced with a fast bitwise AND operator (&) for index wrapping.
If a developer accidentally configures a buffer size that is not a power of two (e.g., 100), the bitwise logic will fail, leading to data corruption. This error must be caught at compile-time, not runtime.
Your task is to implement a Class Template named PowerOfTwoBuffer. It must accept one Non-Type Template Parameter:
int Size: The capacity of the buffer.The class must:
static_assert to validate that Size is a power of two. Use the bitwise check: (Size > 0) && ((Size & (Size - 1)) == 0).int getWrappedIndex(int raw_index) that returns the index wrapped using the bitwise mask: raw_index & (Size - 1).Program Flow:
main function instantiates a PowerOfTwoBuffer with a fixed size of 16 (which is valid).N (number of test indices).N times:raw_index.getWrappedIndex(raw_index).Input Format:
N.N lines: Integer raw_index.Output Format:
Wrapped: <value>Example:
Example 1 (Buffer Size = 16)
Input:
3
5
16
20Output:
Wrapped: 5
Wrapped: 0
Wrapped: 4Explanation:
Constraints:
N range: 1 to 20Size is fixed at 16 for the test cases (to ensure compilation).static_assert.& operator for wrapping (no %).
Input
3 5 16 20
Expected Output
Wrapped: 5 Wrapped: 0 Wrapped: 4