Created
January 28, 2023 13:48
-
-
Save CarloCattano/975a737001a27d402a181883ece890a1 to your computer and use it in GitHub Desktop.
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
/* ************************************************************************** */ | |
/* */ | |
/* ::: :::::::: */ | |
/* bin.c :+: :+: :+: */ | |
/* +:+ +:+ +:+ */ | |
/* By: carlo <[email protected]> +#+ +:+ +#+ */ | |
/* +#+#+#+#+#+ +#+ */ | |
/* Created: 2023/01/28 14:47:40 by carlo #+# #+# */ | |
/* Updated: 2023/01/28 14:47:42 by carlo ### ########.fr */ | |
/* */ | |
/* ************************************************************************** */ | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
int main (void) | |
{ | |
unsigned char c = 'a'; | |
unsigned int i = 1 << sizeof(char) * 8; | |
unsigned char *bins; | |
bins = malloc(sizeof(char) * 8); | |
int iter = 0; | |
while (i > 0) | |
{ | |
if(c & i) | |
{ | |
write(1, "1",1); | |
bins[iter] = '1' - 48; | |
iter++; | |
} | |
else | |
{ | |
write(1, "0", 1); | |
bins[iter] = '0' - 48; | |
iter++; | |
} | |
i /= 2; | |
} | |
printf("\niter %d", iter); | |
iter -= 1; | |
printf("\n--------\n"); | |
iter = 0; | |
while (iter <= 8) | |
{ | |
printf("\n%i", bins[iter]); | |
iter++; | |
} | |
printf("\n--------\n"); | |
printf("\nc %d", c); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment