ANDing char and int data items

R

Rahul

What will be the result of ANDing an integer data item with a
character data item?

#include<stdio.h>
int main()
{
int a = 0xFFFFFFFF;
char c = 0xAA ;

c &=a;
printf("\nc = %x", c);


}

the output that i got is " c=ffffffaa "

How did it happen, can anybody tell me ?
Thanks.
 
L

Lew Pitcher

What will be the result of ANDing an integer data item with a
character data item?

#include<stdio.h>
int main()
{
int a = 0xFFFFFFFF;
char c = 0xAA ;

c &=a;
printf("\nc = %x", c);

}

the output that i got is " c=ffffffaa "

How did it happen, can anybody tell me ?

Twos-complement arithmatic, sign extension, variadic function, integer
data type promotion

Briefly, because printf() is a variadic function, the default
promotion rules apply to all the variadic arguments. This means that
the char variable c that you specify gets promoted to int in it's
passage to printf(). It appears that, on your platform, "char" is
treated as a signed data item, and the promotion of the character
value 0xaa to an int is sign extending it. Your "%x" tells printf() to
print the integer as a hex string, and since the integer (from the
promotion) is 0xffffffaa (because aa has it's high-order bit on, and
is thus negative in 2s complement arithmetic) you get the resulting
"c=ffffffaa" string out.
 
E

Eric Sosman

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.
char c = 0xAA ;

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.
 
K

Kenneth Brody

Rahul said:
What will be the result of ANDing an integer data item with a
character data item?

#include<stdio.h>
int main()
{
int a = 0xFFFFFFFF;
char c = 0xAA ;

c &=a;
printf("\nc = %x", c);

}

the output that i got is " c=ffffffaa "

How did it happen, can anybody tell me ?

(Ignoring the "you forgot the terminating newline for your output,
so it may not display anything" nit-picking.)

Your system uses the signed type of "char". When the char value of
0xaa is promoted to an int for printf, it gets sign-extended to the
value 0xffffffaa. (Your system apparently uses 32-bit ints.)

You can simplify the body of main() down to:

printf("%x\n", (char)0xaa);

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
J

Joe

That's very odd that the prinf would display variable 'c' as a long since it is declared as a char.

Nonetheless, the ANDing only occurred on the least significant byte because the char was not type-cast to a int.

try this to get a better result: a &= (int)c; printf("\na =", %x, a);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top