Search This Blog

Sunday, October 5, 2014

WAP to shift all one's at one side in a given 32-bits number. i.e num = 0xcccccccc o/p - 0xffff0000

1:  main()  
2:  {  
3:    unsigned int num = 0xcccccccc;  
4:    unsigned int res = 0,b1= 0,count=0;  
5:    printf("Num : 0x%x\n",num);  
6:    while(num)  
7:    {  
8:      if(num & 1)  
9:      {  
10:        b1 = (b1 << 1) | 1;  
11:        count++;  
12:      }  
13:      num >>= 1;   
14:    }  
15:    num = b1 << count;  
16:    printf("Result : 0x%x\n",num);  
17:  }   

No comments:

Post a Comment