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;
}
Discussion:
65 comments Page 1 of 7.
Mayur said:
9 years ago
Answer to Why everyone taking for char 2bytes?
we are not taking char as 2 bytes.
Let's picture the real scenario, the memory stored in the register of RAM.
for example, suppose 'i' is stored at 0x00 as shown in the below:
Address values
0x06 0000 0000
0x05 0000 0000
0x04 0000 0000
0x03 0000 0000
0x02 0000 0000
0x01 0000 0000
0x00 1000 0000 <----- i = 0x80;
now if we right shift the value of 'i', then MSB(most significant bit) will shifted to the next register* as shown below,
Address values
0x06 0000 0000
0x05 0000 0000
0x04 0000 0000
0x03 0000 0000
0x02 0000 0000
0x01 0000 0001
0x00 0000 0000 <----- i = 0x00;
It should show 0 if we read the value of 'i' as a character(%C) which is NULL(it will print NULL which is not a visible character like space).
But here we are reading the value of 'i' as integer(%d) so it will read the 4 bytes, which are,
0x03 0000 0000
0x02 0000 0000
0x01 0000 0001
0x00 0000 0000
In single line 00000000 00000000 00000001 00000000 <----- i = 0x100 = 256.
@Gopi Krishna, If we right shift the variable then MSB goes to next register's LSB, not the same one in C language.
One you are describing is the case of the microcontroller/processes @Kunal.
we are not taking char as 2 bytes.
Let's picture the real scenario, the memory stored in the register of RAM.
for example, suppose 'i' is stored at 0x00 as shown in the below:
Address values
0x06 0000 0000
0x05 0000 0000
0x04 0000 0000
0x03 0000 0000
0x02 0000 0000
0x01 0000 0000
0x00 1000 0000 <----- i = 0x80;
now if we right shift the value of 'i', then MSB(most significant bit) will shifted to the next register* as shown below,
Address values
0x06 0000 0000
0x05 0000 0000
0x04 0000 0000
0x03 0000 0000
0x02 0000 0000
0x01 0000 0001
0x00 0000 0000 <----- i = 0x00;
It should show 0 if we read the value of 'i' as a character(%C) which is NULL(it will print NULL which is not a visible character like space).
But here we are reading the value of 'i' as integer(%d) so it will read the 4 bytes, which are,
0x03 0000 0000
0x02 0000 0000
0x01 0000 0001
0x00 0000 0000
In single line 00000000 00000000 00000001 00000000 <----- i = 0x100 = 256.
@Gopi Krishna, If we right shift the variable then MSB goes to next register's LSB, not the same one in C language.
One you are describing is the case of the microcontroller/processes @Kunal.
(1)
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.
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.
Parveen Rohaj said:
1 decade ago
Some are having doubt that why everyone having explanation assuming it is 2 byte?
First clear in you mind that we are not converting 1 byte of character into 2 byte.
Because if we do this then we have to write i = i<<2; but we are just printing a integer value using integer format specifier, but not changing the value of i.
After this program try this.
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
printf("%d\n", i<<1);
printf("%d\n",i);
return 0;
}
I am just want to show that value of i is not changing.
You will get the output as:
256 as value of integer after left shift and 128 as the value of the i.
So don't confuse between both.
First clear in you mind that we are not converting 1 byte of character into 2 byte.
Because if we do this then we have to write i = i<<2; but we are just printing a integer value using integer format specifier, but not changing the value of i.
After this program try this.
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
printf("%d\n", i<<1);
printf("%d\n",i);
return 0;
}
I am just want to show that value of i is not changing.
You will get the output as:
256 as value of integer after left shift and 128 as the value of the i.
So don't confuse between both.
Gopi.v said:
9 years ago
if char i=0,if we use %c than it should print 0 on stdout.
But if we use %d it should print the equal ASCII value of that char.if it is 0 than 48 should print.memory will take on datatype but on formatspecifier provided.any way char should take 1byte to store,for signed char the range from -128 to +127.1 bit used for sign representation. For unsign char no loss of bit ranges from 0 to 255.256 not valid in char type.
in microcontroller suppouse 8051 all registers 8-bit othar dptr(16bit) if accumulator a regester value exceeds 8 bit then it will generate the carry.
But if we use %d it should print the equal ASCII value of that char.if it is 0 than 48 should print.memory will take on datatype but on formatspecifier provided.any way char should take 1byte to store,for signed char the range from -128 to +127.1 bit used for sign representation. For unsign char no loss of bit ranges from 0 to 255.256 not valid in char type.
in microcontroller suppouse 8051 all registers 8-bit othar dptr(16bit) if accumulator a regester value exceeds 8 bit then it will generate the carry.
(1)
Chotu said:
1 decade ago
Dear sutendra Mirajkar
int main()
{
unsigned char i = 0x80;
i=i<<1;// i is char so 0.
0x80 in binary 1000 0000
after letf shift(i<<1) is 0000 0000 .That value store in i char(1byte 00000000)
printf("%d\n", i);// out put is 0
return 0;
}
//////////////////////////////////////
our result in inter thats y i take int var; i<<1 value is store int var. The output is 256
int main()
{
unsigned char i = 0x80;
int var;
var=i<<1;
printf("%d %d %d\n", var);
return 0;
}
int main()
{
unsigned char i = 0x80;
i=i<<1;// i is char so 0.
0x80 in binary 1000 0000
after letf shift(i<<1) is 0000 0000 .That value store in i char(1byte 00000000)
printf("%d\n", i);// out put is 0
return 0;
}
//////////////////////////////////////
our result in inter thats y i take int var; i<<1 value is store int var. The output is 256
int main()
{
unsigned char i = 0x80;
int var;
var=i<<1;
printf("%d %d %d\n", var);
return 0;
}
Apurva Nigam said:
1 decade ago
@Swathi:
to conevrt hex to binary u need to take each digit of hex value and write its binary equivalent.
For eg:-
if hex = 88 , its binary would be
bin = 1000 1000
since binary equivalent of 8(decimal) is 1000.
example2:-
if hex = AF , its binary equi is
bin = 1010 1111
as binary equi of 'A'(in decimal system its 10) 1010 and that of 'F'(in decimal its 15) is 1111
Hope this will help u.
Take care :)
to conevrt hex to binary u need to take each digit of hex value and write its binary equivalent.
For eg:-
if hex = 88 , its binary would be
bin = 1000 1000
since binary equivalent of 8(decimal) is 1000.
example2:-
if hex = AF , its binary equi is
bin = 1010 1111
as binary equi of 'A'(in decimal system its 10) 1010 and that of 'F'(in decimal its 15) is 1111
Hope this will help u.
Take care :)
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.
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.
Kundan Kumar said:
1 decade ago
First you have look what is value of i.
Value of i is 0x80
It means value in Hexadecimal.
So,
Write it's binary in four bit for each digit
For zero(0) 0000.
For eight(8) 1000.
Finally you get this binary in 16-bit 0000 0000 10000 0000.
Now solve this expression i<<1.
Shift one(1) bit after shifting you get this binary
0000 0001 0000 0000 and print it %d(decimal value) i.e 256.
Value of i is 0x80
It means value in Hexadecimal.
So,
Write it's binary in four bit for each digit
For zero(0) 0000.
For eight(8) 1000.
Finally you get this binary in 16-bit 0000 0000 10000 0000.
Now solve this expression i<<1.
Shift one(1) bit after shifting you get this binary
0000 0001 0000 0000 and print it %d(decimal value) i.e 256.
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.
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.
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:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers