The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
52 views

what does putchar(x&m) does in the question?

 

in Programming by Active (3.1k points)
edited by | 52 views
0
Answer is A ??
0
I think option A is correct.
0
yes..how?
+1
n = n & (n-1) // will give 136.

Let's come to the inside the Display function.

Here loop is running from the 31 to 0.

For the first time i = 31.

then m = 2^31.

Perform bitwise & between ( 136 & 2^31) if it is not equal to zero then print 1 otherwise 0.

here will be zero So, it will print 0.

Same result 0 will be printed from 31 to 8.

Here 136 = 1000 1000

Then for the loop i = 7.

m = 2^7 = 128.

( 136 & 128) here result will be 0.

you can further proceed.

The answer will be.

00000000 00000000 00000000 10001000
+1
Let me explain you!!

In main if we apply bitwise & between n and n-1 we will get the value of n as 136 i.e. 10001000.

And new value of n is being passed to display.

In display:

m=1<<i ------ this statement is left shift of 1 by i bits,

so for i = 31 ,

m= 10000000000000000000000000000000

for i = 20

m=1000000000000000000000000000000

...

...

and the statement is getting repeated 32 times

and each time x&m is to calculated.

and by ternary operator either we get 1 or 0...

That's how the answer is A..

Hope this helps!!

Please log in or register to answer this question.

Related questions

0 votes
1 answer
5
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,822 questions
54,735 answers
189,347 comments
80,087 users