binary numbers
divisibility
mathematical tricks
algorithm
number theory

How to know if a binary number divides by 3?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In number theory, determining whether a number is divisible by 3 is an essential concept often explored in the context of modular arithmetic. While you may be familiar with checking divisibility by observing the sum of the digits in a decimal system, do similar rules apply to binary numbers? This article will explore how to identify if a binary number divides by 3, detail the underlying mathematical principles, and provide practical examples.

Understanding Divisibility by 3 in Decimal Numbers

Before delving into binary numbers, let's recap a fundamental rule in decimal numbers: A number is divisible by 3 if the sum of its digits is divisible by 3. For instance, the number 123 has digits that sum to 6 (1 + 2 + 3 = 6), and since 6 is divisible by 3, so is 123.

Divisibility by 3 in Binary Numbers

The approach to checking divisibility by 3 in binary numbers is rooted in modular arithmetic. Here's how it works:

  1. Conversion to Base 10: Convert the binary number to its decimal (base 10) equivalent and then check for divisibility by 3. However, this method can be cumbersome for large binary numbers.
  2. Direct Binary Check: A more efficient method involves using properties specific to binary:
    • Consider the binary number to be the sum of its bits, but adjusted for their positions. In other words, each bit represents a power of 2: for a binary number bn1bn2b1b0b_{n-1}b_{n-2}\ldots b_1b_0, the decimal equivalent is i=0n1bi2i\sum_{i=0}^{n-1} b_i \cdot 2^i.
    • Observe that the powers of 2 modulo 3 have a repeating pattern: • 201(mod3)2^0 \equiv 1 \pmod{3}212(mod3)2^1 \equiv 2 \pmod{3}221(mod3)2^2 \equiv 1 \pmod{3}232(mod3)2^3 \equiv 2 \pmod{3} • and so forth
    • This periodicity has a cycle of 2, which can be leveraged for divisibility checks.

Binary Divisibility Check Algorithm

To determine if a number represented in binary is divisible by 3, apply the following steps:

  1. Initialize a sum with zero.
  2. Iterate over each bit from least significant (rightmost) to most significant (leftmost): • For bit position ii: • Add `bit value * (2^i % 3)` to the sum.
  3. Result Interpretation: If the final sum is divisible by 3, then the binary number is divisible by 3.

Examples

Let's see how the method applies with practical examples:

Example 1: Binary Number `101` (5 in Decimal)

  1. In binary: 101=122+021+120101 = 1 * 2^2 + 0 * 2^1 + 1 * 2^0
  2. Calculate modulo 3 for powers of 2: • 201(mod3)2^0 \equiv 1 \pmod{3}, 221(mod3)2^2 \equiv 1 \pmod{3}
  3. Sum: 11+02+11=21 * 1 + 0 * 2 + 1 * 1 = 2
  4. Since 2 is not divisible by 3, binary `101` is not divisible by 3.

Example 2: Binary Number `110` (6 in Decimal)

  1. In binary: 110=122+121+020110 = 1 * 2^2 + 1 * 2^1 + 0 * 2^0
  2. Calculate modulo 3 for powers of 2: • 201(mod3)2^0 \equiv 1 \pmod{3}, 212(mod3)2^1 \equiv 2 \pmod{3}, 221(mod3)2^2 \equiv 1 \pmod{3}
  3. Sum: 11+12+01=31 * 1 + 1 * 2 + 0 * 1 = 3
  4. Since 3 is divisible by 3, binary `110` is divisible by 3.

Summary

To summarize the discussed mechanism, here is a table of key steps:

StepDescription
InitializeStart with a sum of zero.
IterationProcess each bit with its position's 2i2^i.
Additive ProcessSum up bi×(2i%3)b_i \times (2^i \% 3).
Final CheckIf sum %3==0\% 3 == 0, number is divisible by 3.

Conclusion

Understanding divisibility rules in different numeral systems enriches mathematical insight and problem-solving skills, especially in computing contexts where binary numbers reign supreme. By following the steps outlined, one can effectively determine whether a binary number is divisible by 3, leveraging patterns in modular arithmetic specifically tailored to binary computation.


Course illustration
Course illustration

All Rights Reserved.