C Programming - Bitwise Operators - Discussion

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

int main()
{
    unsigned char i = 0x80;
    printf("%d\n", i<<1);
    return 0;
}
0
256
100
80
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
65 comments Page 3 of 7.

Ishu said:   1 decade ago
Try this it will be all clear.

#include<stdio.h>

int main()
{
unsigned char i = 0x80;
printf("%d\n",(char)(i<<1));
return 0;
}

Chintan said:   1 decade ago
Hello everyone in the syntax of function printf:

printf("format specifier",variables);

In which it would expect the datatype which format specifier specifies not which variable specifies.

Example: printf("%d\n",i<<0x80); will take 2 bytes because format specifier is %d instead if you write.

printf("%c\n",i<<0x80) it will take only one byte and output will be NULL.

Sunil said:   1 decade ago
Hello all,

We are representing 128 in 2 byte because of the followings :

Case 1 - sizeof i=1 \\i is of character data type here.
Case 2 - sizeof 0x80=2 \\ (for 16 bit OS).
Case 3 - sizeof 128=2 \\(for 16 bit OS).
Case 4 - sizeof 'A'=2 \\unsigned char i = 'A';

Case 4 also requires 2 byte to be stored in the memory. So all the R-value(constants) of character take 2 byte from the memory.

Character constants like '0x80' are stored in memory in their corresponding ASCII value,as ASCII values are integer they requires 2 byte of memory.

#include<stdio.h>
int main()
{
unsigned char i = 0x80;
printf("%d\n", i<<1);
printf("%d\n",sizeof 0x80);
printf("%d\n",sizeof i);
return 0;
}

Output
256
2
1

Thanks.

Raj said:   1 decade ago
Not a single explanation was useful. If anyone knows correct explanation, please help us. Thank you.

Subhadeep said:   1 decade ago
What will be the answer if it was printf("%x",i<<1); ?

Sachin mishra said:   1 decade ago
Here it is given unsigned char that's why we have taken 2 byte.

The hex representation of 80 is 1000 0000.

i = 0000 0000 1000 0000.

Left shift by 1.

We get 1= 0000 0001 0000 0000 = 256 as result.

Davil said:   1 decade ago
What is the meaning of "return 0"?

Amr Gamal said:   1 decade ago
Hello All,
Simply,

unsigned char i = 0x80;
printf("%d\n", i<<1);

Produces 256 value as the output of this operation hasn't been stored back in i variable. i variable still contains 0x80 value.

unsigned char i = 0x80;
i=i<<1;
printf("%d\n", i);

This which will produce 0 output.

Anyone found any other thing, please tell me.

Sravan said:   1 decade ago
int 0x86 means it is in hexadecimal So the decimal equivalent of 86 is 128. Left shift of 128 is 256 because left shift by 1 of a number is equivalent to multiplying it with 2.

Jhalak gupta said:   1 decade ago
@sumit %d %c %f are used for printing values in int, char, float respectively and do not do any datatype conversions. Study type casting first then you ll b more clear about your concepts. +whenever any binary operators like +, -. /, * etc are used b/w different data types it. It promotes or demotes the datatype accordingly and then perform operations.


Post your comments here:

Your comments will be displayed after verification.