Reputation: 155
If i write something like 1<<8; whats will be its default type. Is it unsigned or signed by default or i have to type cast it? Basically My question is : What is the TYPE of expressions involving constants(positive) in C (2<<13 for example)? Do Every time we need to typecast it?
If i want to use it(1<<8) as a signed typecasting is mandatory?? like short int x = (signed)(1<<8);
Upvotes: 0
Views: 125
Reputation: 71
1) A char has 8 bits. You have to make 1<<7 to set only the first bit.
2) It doesn't matter if the value is signed or not, the bits are the same. The datatype of the left side of the "=" decides if the value is signed or not.
You can try this example: I had to shift 31 times, because my value is automatically called to int.
#include <stdio.h>
int main() {
printf("size of int in Bytes: %d \n",sizeof(int)); //4
printf("my number: %d \n",1<<31); //-2147483648
printf("my number: %u \n",1<<31); //2147483648
return 0;
}
Maybe you can study this to understand %d and %u: printf reference
Upvotes: 1
Reputation: 223673
The result of 1 << 8
has type int
, but it could have a different type if the 1
were a larger value.
When the source code has a decimal numeral without a suffix, its type is the first of int
, long int
, or long long int
that can represent the value. Because the sizes of these types may differ in different C implementations, the type of a numeral can differ in different C implementations. For example, 65536
may be int
in one C implementation and long int
in another.
The result of a bitwise shift has the type of the (promoted) left operand. (Promotion is not relevant here because every integer literal is already at the type that results from its promotion. However, if you had char x;
, then x
would be subject to promotion when used in an expression.)
Thus, the type of 1 << 8
is int
.
The interaction of types, promotion, and bitwise shifts can be troublesome. Novice programmers may use 1 << 31
to get a value with bit 31 set. However, in many C implementations, this expression has undefined behavior, because the ideal mathematical result, 231, is not representable as an int
in an implementation where int
is 32 bits. The operation overflows, and the C standard does not undefined the behavior.
For this reason, it is common to use unsigned values with bitwise shift operators and with bitwise logical operators. Instead of 1 << 31
, 1u << 31
is preferred.
A particularly nasty problem occurs in unsigned char x; … x << 24
. Here, the programmer thinks they have done the right thing by using an unsigned type for bit operations. Unfortunately, the integer promotion rules (in normal C implementations) convert an unsigned char
to an int
, not to an unsigned int
. Thus, in x << 24
, x
becomes a signed integer, and the shift may overflow, resulting in undefined behavior.
Therefore, it is imperative that any programmer using bitwise operations learn the integer promotion rules and the usual arithmetic conversions, which are documented in the C standard, at C 2011 (N1570) 6.3.1.1 2 and 6.3.1.8 1.
Upvotes: 0
Reputation: 147
The type of the result is the same as the type of the promoted left operand. The default integer signedness is signed so your result will be signed by default.
Upvotes: 5