/*
* File: main.c
* Author: lighta
*
* Created on March 22, 2012, 12:18 PM
*/
#include <stdio.h>
#include <stdlib.h>
/*
* 1101, 1111, 1010 and 1111
<Michy> So i'm trying to mask those onto the 64 bit, but in specific locations
<Michy> so bits 60~64 are 1101, 40~44 are 1111, 20~24 are 1010, and 0~4 are 1111
*/
void BinaryLog(long long a);
long long int domask(long long int foo, int fooindex, int mask, int maskindex);
int main(int argc, char** argv) {
int mask = 0xDAEF; //general mask 1101, 1111, 1110, 1111
int submask = 0xF; //4bit
long long int foo = 0xEDCBA9876543210; //60bit number
int result;
long long int foores;
//testing
/*
printf ("mask=%x newsub1=%x, expected=%x\n",mask,getsubmask(mask,0),0xF);
printf ("mask=%x newsub2=%x, expected=%x\n",mask,getsubmask(mask,4),0xE);
printf ("mask=%x newsub3=%x, expected=%x\n",mask,getsubmask(mask,8),0xa);
printf ("mask=%x newsub4=%x, expected=%x\n",mask,getsubmask(mask,12),0xD);
printf ("foo=%x newsub=%x, expected=%x\n",foo,getfoobit(foo,4),0x1); //overflow for printf
BinaryLog(foo);
BinaryLog(getfoobit(foo,8)); //getting bit 8-12
BinaryLog(0x2); //expectud value
BinaryLog(getfoobit(foo,52)); //getting bit 54:58
BinaryLog(0xD); //expected value
result = getfoobit(foo,52)&getsubmask(mask,4);
printf ("Masking\n %x & %x = %x\n",(int)getfoobit(foo,52),(int) getsubmask(mask,4),result);
result = getfoobit(foo,40)&getsubmask(mask,12);
printf ("Masking\n %x & %x = %x\n",(int)getfoobit(foo,40),(int) getsubmask(mask,12),result);
result = getfoobit(foo,52)&getsubmask(mask,4); //masking foo bit [52:56] with mask bit [4:8]
foores = (long long)result<<54; //storing result
result = getfoobit(foo,40)&getsubmask(mask,12); //masking foo bit [40:44] with mask bit [12:16]
foores |= (long long)result<<40; //adding result to previous one
result = getfoobit(foo,20)&getsubmask(mask,0); //masking foo bit [40:44] with mask bit [12:16]
foores |= (long long)result<<20; //adding result to previous one
*/
//end testing
foores = domask(foo,52,mask,4); //masking foo bit [52:56] with mask bit [4:8]
foores |= domask(foo,40,mask,12); //masking foo bit [40:44] with mask bit [12:16]
foores |= domask(foo,20,mask,0); //masking foo bit [20:24] with mask bit [0:4]
BinaryLog(foores);
return (EXIT_SUCCESS);
}
int getsubmask(int mask, int index){
return (mask&(0xF<<index))>>index;
}
int getfoobit (long long int foo, int index){
return (foo&((long long)0xF<<index))>>index;
}
long long int domask(long long int foo, int fooindex, int mask, int maskindex){
int result = getfoobit(foo,fooindex)&getsubmask(mask,maskindex); //masking foo bit [52:56] with mask bit [4:8]
return (long long)result<<fooindex; //storing result
}
void BinaryLog(long long a)
{
char *str, *tmp;
int cnt = 63;
str = (char *) malloc(65); /*64 + 1 , because its a 64 bit binary number*/
tmp = str;
while ( cnt > -1 )
{
str[cnt]= '0';
cnt --;
}
cnt = 63;
while (a > 0)
{
if (a%2==1)
str[cnt] = '1';
cnt--;
a = a/2 ;
}
printf("Binary output: %s\n", tmp);
free(tmp);
}