By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,694 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,694 IT Pros & Developers. It's quick & easy.

ANDing char and int data items

P: n/a
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.

Mar 14 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Mar 14, 10:42 am, "Rahul" <rahul.kashya...@gmail.comwrote:
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.

Mar 14 '07 #2

P: n/a
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.

--
Er*********@sun.com
Mar 14 '07 #3

P: n/a
Rahul wrote:
>
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:Th*************@gmail.com>

Mar 14 '07 #4

P: n/a
Joe
"Rahul" <ra*************@gmail.comwrote:
>
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);
Mar 14 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.