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.

Sai said:   1 decade ago
Here mask means unsigned int i.e no negative values....mask 01 means 32 bit value.it is "00000000000000000000000000000001"
c=48 it is written as "00000000000000000000000000110000"
now "or" operation is performed then value becomes 49,
mask=mask<<1 means it shifts 1 to 1-bit before then it becomes "000000000000000000000000000000010".now do o operation with 32digit binary bit of 48.

Now value becomes 50.again mask=mask<<1 "00000100"(last 8bits)similarly do till for loop fails..then we get values as 49,50,52,56,48....the %c converts int to char thatmeans it prints the ASCII values of these nunbers ASCII of 48=0,49=1,50=2......57=9..replace the values u get o/p as 12480......

Amey said:   1 decade ago
c=110000
Mask=000001
In for loop
i=1
Print 110000 | 000001 i.e. 110001=49 i.e. ascii for '1'
Hence printing '1'
Now mask<<1 hence mask=000010
i=2
Print 110000 | 000010 i.e. 110010=50 i.e. ascii for '2'
Hence printing '2'
Now mask<<1 hence mask=000100
i=3
Print 110000 | 000100 i.e. 110100=52 i.e. ascii for '4'
Hence printing '4'
Now mask<<1 hence mask=001000
i=4
Print 110000 | 001000 i.e. 111000=56 i.e. ascii for '8'
Hence printing '8'
Now mask<<1 hence mask=010000
i=5
Print 110000 | 010000 i.e. 110000=48 i.e. ascii for '0'
Hence printing '0'
Now mask<<1 hence mask=001000

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.

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)

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)

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

Jayadeep said:   1 decade ago
Hi @arun

Here c is assigned 48 so its binary value is 110000
mask is assigned 1 i.e 00000001

So while running the for loop first time
c|mask means bitwise operation is performed so each bit is manupulated

So c|mask gives

00110000
00000001
--------
00110001 = 49

But we are printing using %c so the ascii character corresponding to this is printed

So 1 is printed

then the mask is left shifted so it becomes 00000010
so if we perform or operation we will get 50 which is equal to 2
like this it prints 12345

Santhosh kumar said:   8 years ago
@Priyanka.

"or" operation performed is not like 1+0=0, 1+1=0, 0+0=0
it is performed using truth table like;

OR truth table
Input Output
1 - 1 1
1 - 0 1
0 - 1 1
0 - 0 0
therefore
the operation on
0011 0000
0001 0000
_________
0011 0000
________
which is equal to 48 whose ascii value is 0.
what you have done is
11
0011 0000
0001 0000
________
0100 0000
_________

So or operation is done using truth table conditions.

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).

Kirubhakaran said:   1 decade ago
It is nothing but shifting the binary values to left !! In case if u have a binary number 11001101, after you left shit this binary number by 1 digit, you will have 10011010.. that is, the first digit is removed and a he remaining elements are written and a zero is appended at last...


Post your comments here:

Your comments will be displayed after verification.