sorry its left shift.. one mistake.. other than this ans is correct.. i have done left shift on 1 by 4 bits that why result became ~(00000.....10000) ignore the right shift i have mentioned..
On Sun, Nov 25, 2012 at 4:48 PM, Rahul Kumar Patle < patlerahulku...@gmail.com> wrote: > @pandey : here you are shifting 1 to right by 4 bit (as rushiraj said > correctly) > which becomes 00000.....10000, then you are doing negation which becomes > 111111...01111(mask) > now do you and operation > mask & 56 = 40 > mask & 64 = 64 > mask & 127 = 111 > you can notice that only 5th lsb (effective value 16) is zero which is > present in 56 and 127 but not in 64 > so 64 remains the same but 56 and 127 got reduced by 16. > > > On Sat, Nov 24, 2012 at 8:36 PM, Dave <dave_and_da...@juno.com> wrote: > >> @Rajesh: In binary, mask = 111...100000 (with 4-byte ints, this is 27 >> 1-bits followed by 5 0-bits). The logical product of num with mask zeros >> out the low order 5 bits while retaining the high order 27 bits. Thus, res >> is num truncated to the largest multiple of 32 that does not exceed num. 56 >> = (1*32 + 24) goes to 1*32 = 32, 64 (=2*32 + 0) stays at 2*32 = 64, and 127 >> (=3*32 + 31) goes to 3*32 = 96. >> >> Dave >> >> On Saturday, November 24, 2012 2:45:24 AM UTC-6, rajesh pandey wrote: >> >>> 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; >>> } >>> >>> please explain the logic behind the output. >>> >>> Thanks, >>> Rajesh >>> >> -- >> >> >> > > > > -- > Thanks and Regards: > Rahul Kumar Patle > M.Tech, School of Information Technology > Indian Institute of Technology, Kharagpur-721302, > India<http://www.iitkgp.ac.in/> > Mobile No: +91-8798049298, +91-9424738542 > Alternate Email: rahulkumarpa...@hotmail.com > [image: > Linkedin]<http://www.linkedin.com/profile/view?id=106245716&trk=tab_pro> > [image: Twitter] <https://twitter.com/rahulkumarpatle> > <https://www.facebook.com/rkpatle> > > -- Thanks and Regards: Rahul Kumar Patle M.Tech, School of Information Technology Indian Institute of Technology, Kharagpur-721302, India<http://www.iitkgp.ac.in/> Mobile No: +91-8798049298, +91-9424738542 Alternate Email: rahulkumarpa...@hotmail.com [image: Linkedin]<http://www.linkedin.com/profile/view?id=106245716&trk=tab_pro> [image: Twitter] <https://twitter.com/rahulkumarpatle> <https://www.facebook.com/rkpatle> --