ANDing char and int data items

Discussion in 'C Programming' started by Rahul, Mar 14, 2007.

  1. Rahul

    Rahul Guest

    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.
     
    Rahul, Mar 14, 2007
    #1
    1. Advertising

  2. Rahul

    Lew Pitcher Guest

    On Mar 14, 10:42 am, "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 ?


    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.
     
    Lew Pitcher, Mar 14, 2007
    #2
    1. Advertising

  3. Rahul

    Eric Sosman Guest

    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.

    --
     
    Eric Sosman, Mar 14, 2007
    #3
  4. 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:>
     
    Kenneth Brody, Mar 14, 2007
    #4
  5. Rahul

    Joe Guest

    "Rahul" <> 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);
     
    Joe, Mar 14, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,221
    Martien Verbruggen
    Jun 27, 2003
  2. trey

    newbie: char* int and char *int

    trey, Sep 10, 2003, in forum: C Programming
    Replies:
    7
    Views:
    405
    Irrwahn Grausewitz
    Sep 10, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,648
    Old Wolf
    Jan 20, 2004
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,061
    Ian Collins
    May 9, 2006
  5. Kyle Schmitt

    anding statements.

    Kyle Schmitt, May 14, 2008, in forum: Ruby
    Replies:
    11
    Views:
    253
    Robert Klemme
    May 14, 2008
Loading...

Share This Page