Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > ANDing char and int data items

Reply
Thread Tools

ANDing char and int data items

 
 
Rahul
Guest
Posts: n/a
 
      03-14-2007
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.

 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      03-14-2007
On Mar 14, 10:42 am, "Rahul" <(E-Mail Removed)> 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 ?


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.



 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-14-2007
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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      03-14-2007
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: <(E-Mail Removed)>

 
Reply With Quote
 
Joe
Guest
Posts: n/a
 
      03-14-2007
"Rahul" <(E-Mail Removed)> wrote:
>


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);
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
int urldecode(char *src, char *last, char *dest) gert C Programming 20 02-16-2007 11:28 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
newbie: char* int and char *int trey C Programming 7 09-10-2003 03:24 AM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments