Sunday, March 22, 2015

bit programming : dynamic bit mask generation using bit programming


bit programming : dynamic bit mask generation using bit programming:
-------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#define mask_gen(num)   ((1<<num)-1)
void bin(unsigned int num)
{
        int bit,i;
        printf("\neight bit binary representation of the given number\n");
        for(i=31;i>=0;i--)
        {  
                bit = num & (1<<i);
                if(bit)
                printf("1");
                else
                printf("0");

        }  
        printf("\n");
}

main()
{
        unsigned int i;

        for(i=0;i<32;i++)
        {  
                printf("\n%d",mask_gen(i));
                bin(mask_gen(i));   
        }  

}

output:

[root@selvakumart bit_pgm]# gcc mask_generation.c -g
[root@selvakumart bit_pgm]# ./a.out

0
eight bit binary representation of the given number
00000000000000000000000000000000

1
eight bit binary representation of the given number
00000000000000000000000000000001

3
eight bit binary representation of the given number
00000000000000000000000000000011

7
eight bit binary representation of the given number
00000000000000000000000000000111

15
eight bit binary representation of the given number
00000000000000000000000000001111

31
eight bit binary representation of the given number
00000000000000000000000000011111

63
eight bit binary representation of the given number
00000000000000000000000000111111

127
eight bit binary representation of the given number
00000000000000000000000001111111

255
eight bit binary representation of the given number
00000000000000000000000011111111

511
eight bit binary representation of the given number
00000000000000000000000111111111

1023
eight bit binary representation of the given number
00000000000000000000001111111111

2047
eight bit binary representation of the given number
00000000000000000000011111111111

4095
eight bit binary representation of the given number
00000000000000000000111111111111

8191
eight bit binary representation of the given number
00000000000000000001111111111111

16383
eight bit binary representation of the given number
00000000000000000011111111111111

32767
eight bit binary representation of the given number
00000000000000000111111111111111

65535
eight bit binary representation of the given number
00000000000000001111111111111111

131071
eight bit binary representation of the given number
00000000000000011111111111111111

262143
eight bit binary representation of the given number
00000000000000111111111111111111

524287
eight bit binary representation of the given number
00000000000001111111111111111111

1048575
eight bit binary representation of the given number
00000000000011111111111111111111

2097151
eight bit binary representation of the given number
00000000000111111111111111111111

4194303
eight bit binary representation of the given number
00000000001111111111111111111111

8388607
eight bit binary representation of the given number
00000000011111111111111111111111

16777215
eight bit binary representation of the given number
00000000111111111111111111111111

33554431
eight bit binary representation of the given number
00000001111111111111111111111111

67108863
eight bit binary representation of the given number
00000011111111111111111111111111

134217727
eight bit binary representation of the given number
00000111111111111111111111111111

268435455
eight bit binary representation of the given number
00001111111111111111111111111111

536870911
eight bit binary representation of the given number
00011111111111111111111111111111

1073741823
eight bit binary representation of the given number
00111111111111111111111111111111

2147483647
eight bit binary representation of the given number
01111111111111111111111111111111
[root@selvakumart bit_pgm]#


No comments:

Post a Comment