Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is char i=0x80 and why overflow did not happen in bit shifting

Tags:

c

Here is a program

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

The output it is giving is 256. I am not clear with what does

unsigned char i=0x80; <-- i is not int it is char so what will it store?

I know bitshift and hexadecimal things. How is the value of i being stored and how does it gets changed to 256?

UPDATE

Why did overflow not occurred when the bit shift operation happened?

like image 544
Registered User Avatar asked Jun 18 '11 18:06

Registered User


People also ask

What does 0X80 mean in C?

0x80 is the most significant bit of an 8-bit byte set. If it is stored in a signed char (on a machine that uses 2's-complement notation - as most machines you are likely to come across will), it is the most negative value (decimal -128); in an unsigned char, it is decimal +128.

What happens when you shift bits?

Logical bit shifting may be useful for multiplying or dividing unsigned integers by powers of two. For example, if the value "0001" or "1" is shifted left, it becomes "0010" or "2," shifted to the left again it becomes "0100," or "4." Shifting to the right has an opposite effect of dividing the value by two per shift.

What format is 0X80?

0X80 is a hexadecimal (hex) number. We can tell it is a hex number because it starts with 0X. Hexadecimal numbers like 0X80 are not often used in daily life, but we see them used for certain things such as html colors, shortening binary numbers, computer error codes, and math exercises.

What is 0xff in C?

Overview. 0xff is a number represented in the hexadecimal numeral system (base 16). It's composed of two F numbers in hex. As we know, F in hex is equivalent to 1111 in the binary numeral system. So, 0xff in binary is 11111111.


2 Answers

In C, a char is an integer type used to store character data, typically 1 byte.

The value stored in i is 0x80 a hexidecimal constant that is equal to 128.

An arithmetic operation on two integer types (such as i << 1) will promote to the wider type, in this case to int, since 1 is an int constant. In any case, integer function arguments are promoted to int.

Then you send the result to printf, with a %d format specifier, which mean "print an integer".

like image 152
sverre Avatar answered Sep 27 '22 21:09

sverre


I think that K&R have the best answer to this question:

2.7 Type Conversions When an operator has operands of different types, they are converted to a common type according to a small number of rules. In general, the only automatic conversions are those that convert a narrower'' operand into awider'' one without losing information, such as converting an integer into floating point in an expression like f + i. Expressions that don't make sense, like using a float as a subscript, are disallowed. Expressions that might lose information, like assigning a longer integer type to a shorter, or a floating-point type to an integer, may draw a warning, but they are not illegal. A char is just a small integer, so chars may be freely used in arithmetic expressions.

So i<<1 converts i to int before it is shifted. Ken Vanerlinde has it right.

like image 39
grok12 Avatar answered Sep 27 '22 21:09

grok12