Nishu said:
If i take a signed integer, so right shifting a negative int should
give me another negative int (signed arithmetic) or a positive integer.
Is this C standard defines or implementation defines?
Implementation. Except that those aren't the only two choices
available to the implementation...
Actually i want to know whether
if(A)
{
printf("operator is arithmetic right shift");
}
else
{
printf(" operator is logical right shift");
}
You are presuming that A = 0xFFFFFFFF will store a negative number
in A. That is a bad assumption:
1) long might have more than 32 bits, in which case 0xFFFFFFFF
would just be a regular signed number. It is not uncommon for long
to be 64 bits with int being 32 bits, short 16 bits, char 8 bits.
But it is also not uncommon for int and long both to be the same size
of 32 bits; there are also a number of compilers for which
long is 32 bits, int is 16 bits...
2) 0xFFFFFFFF is not specifically indicated as a long constant via an 'L'
suffix; interpretation of it starts out by considering it as a
signed int. No negative sign is present in the number, so the
compiler will inspect to determine whether 0xFFFFFFFF fits within
the positive range of signed int on that system; if it does then
0xFFFFFFFF would be considered a positive signed int and there would
then be an implicit cast of that positive signed int into a long for
storage into A; as long is promised to be at least as wide as int,
that would either involve leaving the number unchanged or else
widening it if necessary; widening on most systems would involve
sticking the value in the low bits and zero-filling the upper bits,
but int and long need not have the same internal padding bit structures
so an actual representation change might take place.
If the compiler determines that 0xFFFFFFFF does not fit within
the positive range of signed int, then it would reconsider it as
potentialy being a positive signed long; if it does not fit within
a positive signed long, then it would convert it to unsigned long, which
it should fit into. So you would now have an unsigned long constant
token and you would have a simple long destination to store it into.
The C standard says that if you attempt to store an unsigned
value into a signed location, and the unsigned value fits within
the positive range of the signed type, then the positive value
will be stored -- but it also says that if the unsigned value does
*not* fit within the positive range of the signed type, that the
result of the conversion is up to the implementation. Thus,
long A = 0xFFFFFFFF is not necessarily going to produce a negative
result in A, even if the implementation happens to use 32 bit long.