Created
June 25, 2023 07:08
-
-
Save hamza-cskn/452c5e72b820715f4448ab3a3bdbebc5 to your computer and use it in GitHub Desktop.
bit to string, string to bit serialization.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void right_shift_bit(char *c, int last_bit) { | |
if (last_bit) | |
*c = (*c >> 1) | 0b10000000; | |
else | |
*c = (*c >> 1) & 0b01111111; | |
} | |
int get_bit(char c, int bit_index) { | |
return c >> bit_index & 0b00000001; | |
} | |
int main(int ac, char** av) { | |
char dst[5] = "XXXX"; | |
char src[5] = "ABCD"; | |
int byte_index = 0; | |
while (src[byte_index]) { | |
int bit_index = 0; | |
while (bit_index < 8) { | |
int bit = get_bit(src[byte_index], bit_index); | |
right_shift_bit(dst + byte_index, bit); | |
printf("(%d) %d - %d \n", bit_index, dst[byte_index], bit); | |
bit_index++; | |
} | |
printf(":"); | |
byte_index++; | |
} | |
printf("dst = %s\n", dst); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment