Rahul wrote On 03/14/07 10:42,:
What will be the result of ANDing an integer data item with a
character data item?
#include<stdio.h>
int main()
{
int a = 0xFFFFFFFF;
Warning: Possible implementation-defined behavior here.
On machines where int is narrower than 33 bits, 0xFFFFFFFF
is outside the int range, and the initialization will give
an implementation-defined result or raise an implementaiton-
defined signal.
Warning: Possible implementation-defined behavior here.
On machines where char is signed and is narrower than 9 bits,
0xAA is outside the char range, and the initialization will
give an implementation-defined result or raise an implementation-
defined signal.
c &=a;
printf("\nc = %x", c);
Warning: Implementation-defined behavior here. The argument
corresponding to "%x" must be an unsigned int, but it is
implementation-defined whether the char c promotes to unsigned
int or to plain int. (That is, it is implementation-defined
whether the behavior is defined or undefined.)
}
the output that i got is " c=ffffffaa "
How did it happen, can anybody tell me ?
You are making (or, if you are the same "Rahul" that has
posted similar questions in the past, you are *still* making)
the fundamental error of confusing representations and values.
This is an error easily made, especially with bitwise operators
whose effects are described in representational terms. But
that's only the description, and it's only a convenience: The
bitwise operators, like almost all of C's operators, operate
on the values of their operands and not on their representations.
Therein lies the hint that may help you understand what happened;
for further hints see section 6.3.1.8 of the Standard or look up
"usual arithmetic conversions" in your textbook.
Stop thinking about bits; think about values. Your programs
will be the better for it.