Program Discussion :: Bitwise Operator
344 / 15
Write an efficient program to set the rightmost unset bit.
Answer:
#include
int main()
{
int z = 21;
setUnsitBit(z);
}
void setUnsitBit(int y)
{
int p=y,x = 0;
if (y == 0)
return;
if ((y&(y+1)) == 0)
return;
while (p > 0)
{
if (((y>>x) & 1) == 0)
break;
x++;
p = p/2;
}
printf("%d",(y |=1
Asked In ::
Language:
Ankit
1 Apr, 2018 3:28 PM
#include <stdio.h>
int main()
{
int z = 21;
setUnsitBit(z);
}
void setUnsitBit(int y)
{
int p=y,x = 0;
if (y == 0)
return;
if ((y&(y+1)) == 0)
return;
while (p > 0)
{
if (((y>>x) & 1) == 0)
break;
x++;
p = p/2;
}
printf("%d",(y |=1<<x));
}