C Programming - Bitwise Operators - Discussion

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;
}

[A]. 12400
[B]. 12480
[C]. 12500
[D]. 12556

Answer: Option B

Explanation:

No answer description available for this question.

Chinna said: (Jul 21, 2010)  
To increment five times in i and multiple by c and i and mask.

Ashwin said: (Dec 8, 2010)  
Explanation. Please?

Afsheen said: (Dec 16, 2010)  
Can anyone please explain this. ?

Sai said: (Dec 22, 2010)  
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......

Samatha said: (Mar 21, 2011)  
Well. Thanku.

Sneha said: (May 5, 2011)  
Thanks sai.

Sowmya said: (May 7, 2011)  
Thanks a lot Sai!! :) :)

Yamini said: (Jul 17, 2011)  
I cant understand that left shift operation "<<"
Can anyone pls explain me n make me clear

Kirubhakaran said: (Jul 29, 2011)  
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...

Arun said: (Jul 30, 2011)  
#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;
}

Please explain above the program.

Jayadeep said: (Aug 6, 2011)  
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

Pritam said: (Aug 19, 2011)  
At 5th loop mask=01000 and 48 or 01000 gives 64 then how it comes 48 again?

Razia said: (Aug 19, 2011)  
It is supposed to print c|mask, instead it is printing only the value of mask.

Razia said: (Aug 21, 2011)  
Why it is printing only mask value?

Jhansi said: (Sep 3, 2011)  
Thanks alot sai.

Pawan Mishra said: (Sep 9, 2011)  
Thanks sir realy understoodable answer. Thank you so much.

Adi said: (Sep 13, 2011)  
Razia, its not printing mask.

Its printing 49 as 49-48=1
then 50-48=2
then 52-48=4 and
then 48-48=0 on it prints 12480.

Poobal said: (Sep 13, 2011)  
Please any one can explan this program clearly ?

Bhavana said: (Sep 13, 2011)  
Well said. For clearing my confusion.

Lavanya Duddu said: (Mar 15, 2012)  
<< means Leftshoft operations
lets take number 2 (0010) if you do left shift operation it becomes 0100(4)
so short cut for << operation is just multiply by two.

Rohit said: (Apr 7, 2012)  
@Sai. Thank you very nuch.

Ravi said: (May 2, 2012)  
Thanks sai.

Mun said: (May 10, 2012)  
Left shift << is Multiply by power of 2.
So therefore ,12480 consecutively 5 times !!

Ajit Yadav said: (May 25, 2012)  
For those having problem in 5th loop.
C = 48 = 110000.
Mask= 16 = 010000.
Perform or operation and you will get 110000 i.e. 48.
And 48 in ascii is '0'.

Bhavesh said: (May 30, 2012)  
"c|mask" what does it mean?

Amey said: (Jun 20, 2012)  
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

Pramod Soni said: (Jul 7, 2012)  
Thank you very much Amey.

Vraj Shah said: (Jul 8, 2012)  
Thanks amey :).

Ruella said: (Aug 18, 2012)  
Amey that was impressive and neat! thanks

Gayatree Verma said: (Aug 24, 2012)  
Thanks amey and sai.

Rahul Garg said: (Aug 26, 2012)  
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

Nithy said: (Aug 27, 2012)  
Doesn't masking mean AND operation?

K.Harika said: (Aug 31, 2012)  
Please give the correct answer properly.I can't understand this program.

Sanjana said: (Sep 7, 2012)  
Thanks amey your explanation is very good.

Thirumalai Kumarasamy said: (Apr 4, 2013)  
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.

Ginna said: (May 30, 2013)  
How to find binary values during interview tests?

Kaveri said: (Sep 24, 2013)  
How to calculate ASCII value?

Sachin Behera said: (Jan 31, 2014)  
How can we know quickly the ASCII value ?

Sagalakala Vallavan said: (Jan 31, 2014)  
48 ascii value 0.
49 - 1.
50 - 2.
52 - 4.
56 - 8.
Left shift 0.

Ramkumar said: (Jul 30, 2014)  
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.

Jasss said: (Aug 11, 2014)  
#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?

Tanu Priya Saxena said: (Aug 21, 2014)  
Mask is declared 01. It should be read as an octal since it is starting with 0.

Pankaj said: (Oct 3, 2014)  
#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.

Priyanka said: (Oct 5, 2014)  
For last loop.
i=5;

0011 0000
|0001 0000
=0100 0000 its 64.

how come its 48?

Venu Gopal said: (Oct 13, 2014)  
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).

Mukunda Saini said: (May 27, 2015)  
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?

Jitendra Malviya said: (Aug 15, 2015)  
Mask is assigned 1 i.e. 00000001, means? I am not getting point.

Swati said: (Aug 31, 2015)  
How 48 ASCII is 0? Please anyone explain it?

Mukund said: (Sep 26, 2015)  
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.

Prajkta said: (Jan 19, 2016)  
Please detail explanation this program.

Jitendra Vaishnav said: (Jul 25, 2016)  
@Mukunda Saini.

OR operation doesn't means sum its logical or operation.

Where 1 OR 1 is 1. So there will be no carry on next position.

Rohit Singh said: (Jul 25, 2016)  
Thanks, @Sai & @Thirumalai.

Vasundhara said: (Aug 21, 2016)  
Thanks, easy explanations @Amey.

Shravan said: (Jan 19, 2017)  
@Sai.

Very good explanation. Thank you.

Hamilton said: (Mar 3, 2017)  
Thanks for all your explanation.

Merlin said: (Jun 10, 2017)  
Good Explanation, thanks @Amey.

Komal said: (Jul 11, 2017)  
Very nice, Thank you all.

Dinesh said: (Aug 24, 2017)  
Can we assign an integer value in char data type?

Maheswari said: (Oct 25, 2017)  
Yes, you can assigned integer value in char data type.

For example :
char i=21;
printf("%c",i); //it will not print anything and no error;
printf("%d",i);//it will print the value of I;

Jayesh said: (Dec 24, 2017)  
Thanks, nice explanation @Amey.

Santhosh Kumar said: (Jan 5, 2018)  
@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.

Zhongshunchao said: (Mar 7, 2018)  
0011 0000 == 48 ---0.
0011 0001 == 49 ---1.
0011 0010 == 50 ---2.
0011 0100 == 52 ---4.
0011 1000 == 56 ---8.
0011 0000 == 48 ---0.

Zhongshunchao said: (Mar 7, 2018)  
0011 0000 == 48 ---0
0011 0001 == 49 ---1 48|0001.
0011 0010 == 50 ---2 48|0010.
0011 0100 == 52 ---4 48|0100.
0011 1000 == 56 ---8 48|1000.
0011 0000 == 48 ---0 48|0000.

Neha said: (May 10, 2018)  
Thanks @Amey.

Ruhee said: (May 22, 2018)  
Thanks @Jayadeep.

Padma said: (Jul 21, 2018)  
Thank You @Sai.

Dhivya said: (Oct 2, 2018)  
How to find ascii value manually?

Please explain.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.