C Programming - Bitwise Operators - Discussion

Discussion Forum : Bitwise Operators - Find Output of Program (Q.No. 7)
7.
What will be the output of the program?
#include<stdio.h>

int main()
{
    char c=48;
    int i, mask=01;
    for(i=1; i<=5; i++)
    {
        printf("%c", c|mask);
        mask = mask<<1;
    }
    return 0;
}
12400
12480
12500
12556
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
70 comments Page 4 of 7.

Ramkumar said:   1 decade ago
Left shift operation like an multiples of 2. For ex. If we left shift of 2 at one time the result will be 2*2 = 4. Similarly, right shift operation like an divide by 2. For ex. If we right shift of 2 at one time the result will be 2/2 = 1.

Sagalakala vallavan said:   1 decade ago
48 ascii value 0.
49 - 1.
50 - 2.
52 - 4.
56 - 8.
Left shift 0.

Sachin Behera said:   1 decade ago
How can we know quickly the ASCII value ?

Kaveri said:   1 decade ago
How to calculate ASCII value?

Ginna said:   1 decade ago
How to find binary values during interview tests?

Thirumalai kumarasamy said:   1 decade ago
int main()
{
char c=48;
int i, mask=01;
for(i=1; i<=5; i++)
{
printf("%c", c|mask);
mask = mask<<1;
}
return 0;
}

Yes I understood clearly.

On first time "c|mask"

48 into binary 110000
000001
= 110001 (49 in ascii = 1 next line left shift of mask<<000001 = 000010).

2) 110000 or 000010 = 110010(50=2 in ascii).

3 110000 or 000100 = 110100(52=4 in ascii).

4)110000 or 001000 = 110010(56=8 in ascii).

5)110000 or 010000 = 110000(48=0 in ascii).

Then print one by one 12480.
(1)

Sanjana said:   1 decade ago
Thanks amey your explanation is very good.

K.Harika said:   1 decade ago
Please give the correct answer properly.I can't understand this program.

Nithy said:   1 decade ago
Doesn't masking mean AND operation?

Rahul Garg said:   1 decade ago
Now i am discuss about output 12480 because:
char c=48 ;its means its char value is 0
int mask=1;
there for:
c|mask(in case of%c)=48(110000)|1(000001)=49(its char value)=1
mask=mask<<1(use formula mask*2^1) mask=2;
now mask value is 2 in loop
therefor
c|mask= 110000
or 000010
---------
110010=50(its char value)=2
mask=2*2^1(from formula)new mask value=4
c|mask= 110000
000100
----------
110100=52 its char value=4
mask=4*2^1=8;
continue....till condition false
output=124800


Post your comments here:

Your comments will be displayed after verification.