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:
64 comments Page 1 of 7.
Namu said:
4 years ago
How 0x80 is converted into binary?
(2)
Jasber said:
4 years ago
i=0x80 means( 0000 0000 1000 0000).
i<<1means (0000 0001 0000 0000)= 256.
i<<1means (0000 0001 0000 0000)= 256.
(3)
Keziahtabraham said:
5 years ago
0*80 means 0000 0000 1000 0000.
Then, one time left shift(i<<1):0000 0001 0000 0000
According to position 1 is at 8th position, then to convert binary to decimal, 2^8=256.
Then, one time left shift(i<<1):0000 0001 0000 0000
According to position 1 is at 8th position, then to convert binary to decimal, 2^8=256.
(1)
Rajani R said:
5 years ago
Please someone explain this solution.
ANUPA A said:
6 years ago
Please, someone clearly explain to me what does the bitwise operator<< actually does.
Vaishnavi said:
6 years ago
Kindly explain this program clearly.
Sandeep Kumar said:
6 years ago
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
i=i<<1;
printf("%d\n", i);
return 0;
}
In this case, also typecast operation is performed but final data is stored in 8 bit so the output is 0.
int main()
{
unsigned char i = 0x80;
i=i<<1;
printf("%d\n", i);
return 0;
}
In this case, also typecast operation is performed but final data is stored in 8 bit so the output is 0.
(3)
Nagendra said:
7 years ago
// 1
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
printf("%d\n", i<<1);
return 0;
}
Here output 256.
// 2
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
i=i<<1;
printf("%d\n", i);
return 0;
}
Here output 0.
* The diff is assignment of variable i.
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
printf("%d\n", i<<1);
return 0;
}
Here output 256.
// 2
#include<stdio.h>
int main()
{
unsigned char i = 0x80;
i=i<<1;
printf("%d\n", i);
return 0;
}
Here output 0.
* The diff is assignment of variable i.
(2)
Gopi.v said:
7 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.
Mayur said:
7 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)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers