-
Notifications
You must be signed in to change notification settings - Fork 0
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.
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;
}
Let's consider an example with x = 13
.
-
Binary Representation of 13:
- 13 in binary is
00001101
.
- 13 in binary is
-
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 operationx & (1 << i)
. - If the bit is set, it places '1' in the corresponding position in the result array; otherwise, it places '0'.
-
Initial Values:
-
x = 13
(binary00001101
) -
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:
-
i = 7
:-
(1 << 7)
shifts1
to the left by 7 positions:10000000
-
x & (10000000)
is00000000
(false), sores[0] = '0'
-
-
i = 6
:-
(1 << 6)
shifts1
to the left by 6 positions:01000000
-
x & (01000000)
is00000000
(false), sores[1] = '0'
-
-
i = 5
:-
(1 << 5)
shifts1
to the left by 5 positions:00100000
-
x & (00100000)
is00000000
(false), sores[2] = '0'
-
-
i = 4
:-
(1 << 4)
shifts1
to the left by 4 positions:00010000
-
x & (00010000)
is00000000
(false), sores[3] = '0'
-
-
i = 3
:-
(1 << 3)
shifts1
to the left by 3 positions:00001000
-
x & (00001000)
is00001000
(true), sores[4] = '1'
-
-
i = 2
:-
(1 << 2)
shifts1
to the left by 2 positions:00000100
-
x & (00000100)
is00000100
(true), sores[5] = '1'
-
-
i = 1
:-
(1 << 1)
shifts1
to the left by 1 position:00000010
-
x & (00000010)
is00000000
(false), sores[6] = '0'
-
-
i = 0
:-
(1 << 0)
shifts1
to the left by 0 positions:00000001
-
x & (00000001)
is00000001
(true), sores[7] = '1'
-
After the loop, res
is:
res = {'0', '0', '0', '0', '1', '1', '0', '1', '\0'}
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.