viewing paste bitmasking ex | C

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
/* 
 * 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);
}
Viewed 1362 times, submitted by lighta.