1-bit and 2-bit Characters in Python

In computers, everything is stored in the form of bits, i.e., the smallest pieces of data that can be either 0 or 1. When we talk about 1-bit or 2-bit characters, we mean how many of these bits are used to make a single character (like a letter or symbol).

  • A 1-bit character is just a single 0. It counts as one character by itself.
  • A 2-bit character is made of two bits and can be either 10 or 11.

If we are given a list of bits (containing only 0s and 1s) that ends with 0. The list represents a sequence of characters encoded in either 1-bit or 2-bit format.

The task is to figure out if the last 0 in the array is a standalone 1-bit character or if it is part of a 2-bit character in Python.

Problem Examples

Example 1

# Input: bits = [1, 0, 0]
# Expected Output: True
# Explanation: The bits represent 10 (2-bit) and 0 (1-bit)
bits = [1, 0, 0]
print("Bits sequence:", bits)
print("Last character is 1-bit:", True)
Bits sequence: [1, 0, 0]
Last character is 1-bit: True

Example 2

# Input: bits = [1, 1, 1, 0]
# Expected Output: False
# Explanation: The bits are read as 11 and 10 (both 2-bit characters)
bits = [1, 1, 1, 0]
print("Bits sequence:", bits)
print("Last character is 1-bit:", False)
Bits sequence: [1, 1, 1, 0]
Last character is 1-bit: False

Algorithm to Solve the Problem

To determine whether the last character is a 1-bit character, follow these steps:

  • Initialize a pointer i = 0. This will help us move through the list of bits.
  • Loop through the list until you reach the second-last bit (i.e., while i < len(bits) - 1):
  • If bits[i] == 0, it's a 1-bit character. Move i ahead by 1 step.
  • If bits[i] == 1, it's a 2-bit character. Move i ahead by 2 steps.
  • After the loop, check if i is exactly equal to the last index. If yes, return True; otherwise, return False.

Python Implementation

def isOneBitCharacter(bits):
    # Start from the first bit
    i = 0
    
    # Loop through the bits until just before the last one
    while i < len(bits) - 1:
        # If we find a 1, it's a 2-bit character (either 10 or 11)
        if bits[i] == 1:
            i += 2  # Skip the next bit as well
        else:
            # If we find a 0, it's a 1-bit character
            i += 1  # Move one step ahead
    
    # Check if we are exactly at the last bit
    # If yes, the last character is a 1-bit character
    return i == len(bits) - 1

# Test cases
bits1 = [1, 0, 0]
bits2 = [1, 1, 1, 0]
bits3 = [0]

print("Output for [1, 0, 0]:", isOneBitCharacter(bits1))
print("Output for [1, 1, 1, 0]:", isOneBitCharacter(bits2))
print("Output for [0]:", isOneBitCharacter(bits3))
Output for [1, 0, 0]: True
Output for [1, 1, 1, 0]: False
Output for [0]: True

How It Works

The algorithm simulates reading the bit sequence character by character:

  • When we encounter 0, we know it's a complete 1-bit character
  • When we encounter 1, we know it starts a 2-bit character, so we skip the next bit
  • If we land exactly on the last position, the final 0 is a standalone 1-bit character
  • If we overshoot, the final 0 was part of a 2-bit character

Conclusion

This problem demonstrates how to parse bit sequences with variable-length encoding. The key insight is to simulate the parsing process and check where we end up relative to the last bit position.

Updated on: 2026-03-25T08:48:04+05:30

796 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements