Skip to content

format_binary

Mallikarjunarao Kosuri edited this page Jul 14, 2024 · 1 revision

The format_binary function converts an 8-bit unsigned integer into its binary string representation. Here's a step-by-step explanation of how the function works, along with an example.

Function Explanation

char* format_binary(uint8_t x)
{
    static char res[9]; // Array to hold the binary string plus the null terminator
    for (int i = 7; i >= 0; i--) { // Loop from 7 to 0 (bits in a byte)
        res[7 - i] = (x & (1 << i)) ? '1' : '0'; // Check if the ith bit is set
    }
    res[8] = '\0'; // Null-terminate the string
    return res;
}

Example

Let's consider an example with x = 13.

  1. Binary Representation of 13:

    • 13 in binary is 00001101.
  2. Bitwise Operations:

    • The function iterates from bit position 7 to 0.
    • For each bit position i, it checks if the bit is set using the bitwise AND operation x & (1 << i).
    • If the bit is set, it places '1' in the corresponding position in the result array; otherwise, it places '0'.

Step-by-Step Execution

  • Initial Values:
    • x = 13 (binary 00001101)
    • res is initially an array of 9 characters, all set to \0.
// res = {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'}
  • Loop Iteration:
for (int i = 7; i >= 0; i--) {
    res[7 - i] = (x & (1 << i)) ? '1' : '0';
}
  • Detailed Iterations:
  1. i = 7:

    • (1 << 7) shifts 1 to the left by 7 positions: 10000000
    • x & (10000000) is 00000000 (false), so res[0] = '0'
  2. i = 6:

    • (1 << 6) shifts 1 to the left by 6 positions: 01000000
    • x & (01000000) is 00000000 (false), so res[1] = '0'
  3. i = 5:

    • (1 << 5) shifts 1 to the left by 5 positions: 00100000
    • x & (00100000) is 00000000 (false), so res[2] = '0'
  4. i = 4:

    • (1 << 4) shifts 1 to the left by 4 positions: 00010000
    • x & (00010000) is 00000000 (false), so res[3] = '0'
  5. i = 3:

    • (1 << 3) shifts 1 to the left by 3 positions: 00001000
    • x & (00001000) is 00001000 (true), so res[4] = '1'
  6. i = 2:

    • (1 << 2) shifts 1 to the left by 2 positions: 00000100
    • x & (00000100) is 00000100 (true), so res[5] = '1'
  7. i = 1:

    • (1 << 1) shifts 1 to the left by 1 position: 00000010
    • x & (00000010) is 00000000 (false), so res[6] = '0'
  8. i = 0:

    • (1 << 0) shifts 1 to the left by 0 positions: 00000001
    • x & (00000001) is 00000001 (true), so res[7] = '1'

Final Result

After the loop, res is:

res = {'0', '0', '0', '0', '1', '1', '0', '1', '\0'}

Markdown Diagram

Here is a textual representation of the process:

x = 13 (00001101 in binary)

    7   6   5   4   3   2   1   0
   --- --- --- --- --- --- --- ---
    0   0   0   0   1   1   0   1  (res)

Shift & Check Operations:

1. i = 7: (1 << 7) = 10000000, 13 & 10000000 = 00000000 -> res[0] = '0'
2. i = 6: (1 << 6) = 01000000, 13 & 01000000 = 00000000 -> res[1] = '0'
3. i = 5: (1 << 5) = 00100000, 13 & 00100000 = 00000000 -> res[2] = '0'
4. i = 4: (1 << 4) = 00010000, 13 & 00010000 = 00000000 -> res[3] = '0'
5. i = 3: (1 << 3) = 00001000, 13 & 00001000 = 00001000 -> res[4] = '1'
6. i = 2: (1 << 2) = 00000100, 13 & 00000100 = 00000100 -> res[5] = '1'
7. i = 1: (1 << 1) = 00000010, 13 & 00000010 = 00000000 -> res[6] = '0'
8. i = 0: (1 << 0) = 00000001, 13 & 00000001 = 00000001 -> res[7] = '1'

Final binary string: "00001101"

This function is useful for debugging and visualizing the binary representation of a number, which can be particularly helpful in embedded systems programming where bitwise operations are frequently used.

Clone this wiki locally