Tuesday, March 12, 2013

Set HSB of Unsigned Integer Zero


On My one of the Interview Stint with Adobe, I read a programming or technical puzzle by Mr. Denis ZabavchikI have also been recommended this puzzle by my seniors presently scattered in Sunny Vale, Palo Alto, and Bangalore.
The question is to  set the highest significant bit of an unsigned integer to zero. You are intended to write a macro for that. Comeon, Techies. Get the Job Done!!

Solution:
 #define Set_HSB_ZERO(h) \ 
(h&=(h>>1)|(h>>2), \
 
h|=(h>>2), \
 
h|=(h>>4), \
 
h|=(h>>8), \
 
h|=(h>>16))
Smile!!

2 comments:

  1. #include
    #include

    int main()
    {
    int i = 1<<15;
    int h = 7;
    while((h&i)==0)
    {
    i>>=1;
    }
    h&=~i;
    printf("%d",h);
    return 0;
    }

    ReplyDelete
  2. Ceaselessly steady and an exceptional sense of duty regarding the universe of bloggers.
    Resumeyard

    ReplyDelete