Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Flip One Bit

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Flip One Bit. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Abbott device protocols such as FreeStyle Libre may encode sensor status and measurement flags in individual bits of a byte. Given an unsigned 8-bit value and a bit position, flip exactly that bit and return the resulting byte.

Bit positions are numbered from 0 through 7, where position 0 is the least significant bit. Flipping means changing 0 to 1 or 1 to 0, while leaving every other bit unchanged.

Formal Specification

Implement flip_bit(byte_value, bit_position).

  • Input: byte_value, an integer from 0 through 255, and bit_position, an integer from 0 through 7.
  • Output: An integer from 0 through 255 representing byte_value after the selected bit is toggled.
  • The input is valid, so no error handling is required.

Constraints

  • 0 <= byte_value <= 255
  • 0 <= bit_position <= 7
  • Exactly one bit is flipped
  • All unselected bits remain unchanged

Function Signature

def flip_bit(byte_value, bit_position):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output