Skip to content

Instantly share code, notes, and snippets.

@andfoy
Created April 9, 2015 17:35
Show Gist options
  • Save andfoy/649d8798da368667ed08 to your computer and use it in GitHub Desktop.
Save andfoy/649d8798da368667ed08 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const char* byte_to_binary(int x)
{
static char b[9];
b[0] = '\0';
int z;
for (z = 128; z > 0; z >>= 1)
{
strcat(b, ((x & z) == z) ? "1" : "0");
}
return b;
}
char* set_bits(char* bitmap, int index, char* msg)
{
char x = msg[index];
printf("Value: %s\n", byte_to_binary(x));
char y = x >> 5;
printf("Left Shift: %s\n", byte_to_binary(y)); // 3 → 00000011
printf("R: %s\n", byte_to_binary(bitmap[0]));
bitmap[0] |= y;
printf("Bitwise or: %s\n", byte_to_binary(bitmap[0]));
y = x << 3;
y >>= 5;
if(y == 0)
{
printf("Next Left Shift: %s\n", byte_to_binary(y));
printf("G: %s\n", byte_to_binary(bitmap[1]));
bitmap[1] >>= 3;
bitmap[1] <<= 3;
printf("Replacement: %s\n", byte_to_binary(bitmap[1]));
}
unsigned char mask = 0;
y = x << 6;
y >>= 5;
y |= mask;
index++;
printf("Mask: %s\n", byte_to_binary(y));
x = msg[index];
char z = x >> 7;
printf("Submask: %s\n", byte_to_binary(z));
y |= z;
printf("Value: %s\n", byte_to_binary(x));
printf("Next shift: %s\n", byte_to_binary(y));
printf("B: %s\n", byte_to_binary(bitmap[2]));
bitmap[2] |= y;
printf("Replacement: %s\n", byte_to_binary(bitmap[2]));
}
int main()
{
/* unsigned char x = 97; // a 97 → 01100001*/
/* printf("%c\n", x);*/
/* unsigned char y = 1;*/
/* unsigned int n = 2;*/
/* x |= (1 << n);*/
/* printf("%c\n", x);*/
/* printf("%d\n", (int) sizeof(unsigned char));*/
unsigned char *s;
s = (unsigned char*) calloc(12, sizeof(unsigned char));
s[0] = 154;
s[1] = 179;
s[2] = 216;
s[3] = 179;
s[4] = 27;
s[5] = 86;
s[6] = 63;
s[7] = 1;
s[8] = 67;
s[9] = 227;
s[10] = 31;
s[11] = 88;
for(int i = 0; i < 12; i++)
{
printf("Index: %d, Value: %d\n", i, s[i]);
}
char a[2];
strcpy(a, "am");
for(int i = 0; i < 2; i++)
{
printf("%d\n", a[i]);
}
int n_space = sizeof(a)*sizeof(char)*8 / 3;
printf("Space: %d\n", n_space);
set_bits(s, 0, a);
for(int i = 0; i < 12; i++)
{
printf("Index: %d, Value: %d\n", i, s[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment