DIR Return Create A Forum - Home
---------------------------------------------------------
techsuns
HTML https://techsuns.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: OutPut of a Program
*****************************************************
#Post#: 258--------------------------------------------------
output + logic
By: dinesh Date: November 28, 2012, 8:36 am
---------------------------------------------------------
explain the logic also...
void dosomething(int num)
{
int mask=~(1<<5-1);
int res=num&mask;
printf("%d",res);
}
int main()
{
dosomething(56);
dosomething(64);
dosomething(127);
return 0;
}
#Post#: 265--------------------------------------------------
Re: output + logic
By: adi Date: November 29, 2012, 8:36 am
---------------------------------------------------------
Here mask is used to extract and turn off a particular bit : 5th
bit
The bitwise shift has lower precedence over subtraction operator
so mask looks like 11101111
Now you and it to turn off the bit
#Post#: 266--------------------------------------------------
Re: output + logic
By: ravusairam Date: November 29, 2012, 10:01 pm
---------------------------------------------------------
basically we have to turn off the 5th lsb, and which is 16(so
output of )
which means if we have 5 th bit set we have to put it off.
In 56 , 5 th bit is on, so if it is not there, it is 56 -16 =
40.
similarly remaining also
56 ->40
64 -> 64
127 - > 111
#Post#: 268--------------------------------------------------
Re: output + logic
By: kranthipls Date: November 29, 2012, 10:13 pm
---------------------------------------------------------
Now let us generalize this.............
Here in this example program inorder to put off the 5th bit the
mask is used as mask = ~(1<<5-1)...
Suppose you want to mask the nth bit you can do it by taking the
mask = ~(1<<n-1)
#Post#: 269--------------------------------------------------
Re: output + logic
By: sai124 Date: November 29, 2012, 10:14 pm
---------------------------------------------------------
Answer is
1)when we call dosomething(56) we get output =40
reason:mask is ~(1<<5-1)=~(1<<4)=~(00010000)=11101111 ;
res=11101111&00111000=00101000=40
2)when we call dosomething(64) we get output=64
reason : res=11101111&01000000=01000000=64
3) when we call dosomething(127) we get output=111
reason: res=11101111&01111111=01101111=111
*****************************************************