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 1 of 7.

AVJagjeet said:   5 years ago
@Sai.

As per my knowledge, the explanation is.

itr 1 0001 | 110000 => 49 => ifsc printing %c so convert int to char by ifsc i.e. 1.

itr 2 0010 | 110000 => 50 => ifsc printing %c so convert int to char by ifsc i.e. 2.

itr 3 0100 | 110000 => 110100 => 52 => ifsc printing %c so convert int to char by ifsc i.e. 4.

itr 4 00001000 | 110000 => 111000 => 56 => ifsc printing %c so convert int to char by ifsc i.e. 8.

itr 5 00010000 | 110000 => 110000 => 48 => ifsc printing %c so convert int to char by ifsc i.e. 0.
(14)

Srujana said:   5 years ago
Thanks all for explaining.
(2)

Varsha said:   6 years ago
Thanks @Sai, @Jayadeep and @Amey.
(1)

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)

Rohit Singh said:   9 years ago
Thanks, @Sai & @Thirumalai.

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

Prajkta said:   10 years ago
Please detail explanation this program.

Mukund said:   10 years ago
48- ASCII value is 0.

48 | 01 = 49 value is 1 printed.

Mask << 1 means (mask) 1*2^(1) = 2.

Again 48|2 value 2 will printed.

Mask = 2*2 = 4.

48|4 then 4 is printed after 8 and 0 are printed.

Swati said:   10 years ago
How 48 ASCII is 0? Please anyone explain it?

Jitendra Malviya said:   1 decade ago
Mask is assigned 1 i.e. 00000001, means? I am not getting point.


Post your comments here:

Your comments will be displayed after verification.