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

Mukunda saini said:   1 decade ago
I think OR operation means 1+1 = Sum 0 and carry 1. So carry will add to the next position. 110000+010000 = 1000000. Please tell me how to get last one 48?

Venu Gopal said:   1 decade ago
There is nothing in left shift(<<) and right shift(>>) operators.
Let us discuss in simple manner.

1. Left Shift operator(<<):-

For example if the left shift operator in this manner i<<n the evaluation will be i<<n=i*(2^n).

2. Right shift operator(>>).

i>>n=i/(2^n).

Ex::2<<3=2*(2^3)=2*(2*2*2)=2*(8)=16.(ans).
Ex::2>>3=2/(2^3)=2/(2*2*2)=2/(8)=0.(ans).

Priyanka said:   1 decade ago
For last loop.
i=5;

0011 0000
|0001 0000
=0100 0000 its 64.

how come its 48?

Pankaj said:   1 decade ago
#include<stdio.h>

int main()
{

char c=48; // Integer value of c is 48, which belongs to char '0';
int i, mask=01; // same as mask=1;
printf("%c\n",c);
for(i=1; i<=5; i++)
{
printf("%d %d\n",c,mask); // displaying the integer values of c and mask

printf("%c\n", c|mask);
mask = mask<<1;
}
return 0;
}

Since the bitwise operator is defined on the integer values, i.e a|b, where a and b are integers. In this problem, we calculate bitwise OR on integer values and then convert it back to char as the output is required as "%c". Try the above code, Hope this will help.

Tanu Priya Saxena said:   1 decade ago
Mask is declared 01. It should be read as an octal since it is starting with 0.

Jasss said:   1 decade ago
#include<stdio.h>

int main()
{
int i=4, j=8;
printf("%d, %d, %d\n", i|j&j|i, i|j&j|i, i^j);
return 0;
}

Can anyone tell me how this program will work step by step?

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 ?

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


Post your comments here:

Your comments will be displayed after verification.