63. 8-Bit Register Class

Create a class Reg8 that simulates an 8-bit hardware register.

Class Requirements:

The class must:

  1. Maintain a private uint8_t register value
  2. Provide the following methods:
    • Reg8() → initializes the register to 0
    • void write(uint8_t v) → writes a new value to the register
    • uint8_t read() → returns the current register value
    • void setBit(int bit) → sets the specified bit only if bit is in range 0–7
    • void clearBit(int bit) → clears the specified bit only if bit is in range 0–7

⚠️ If bit is outside the range 0–7, the register must remain unchanged.

In main():

  1. Read three integers:
    • initialValue
    • bitToSet
    • bitToClear
  2. Write initialValue to the register
  3. Set bitToSet
  4. Clear bitToClear
  5. Print the final register value as an integer

Example Input:

10
1
3

Example Output:

2

Explanation:

  • initialValue = 10 = 00001010₂
  • Set bit 1 → already set → unchanged
  • Clear bit 300000010₂
  • Final value = 2

Constraints:

  • Register value must remain private
  • Only class methods may modify bits
  • Bit operations must be ignored for invalid indices
  • Output must be exactly one integer

 

 

 

Loading...

Input

10 1 3

Expected Output

2