Skip to content

Instantly share code, notes, and snippets.

@CarloCattano
Created January 28, 2023 13:48
Show Gist options
  • Save CarloCattano/975a737001a27d402a181883ece890a1 to your computer and use it in GitHub Desktop.
Save CarloCattano/975a737001a27d402a181883ece890a1 to your computer and use it in GitHub Desktop.
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: carlo <ccattano@student.42Berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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