473,378 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

ANDing char and int data items

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
4 4322
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
2
by: Chris Thompson | last post by:
Hi I'm writing a p2p client for an existing protocol. I used a std::vector<char> as a buffer for messages read from the server. The message length is the first 4 bytes. The message code the...
30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
11
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I've noticed a few threads (full of sound and fury, signifying nothing) here recently about allocation of large memory blocks. I'm about to start on a personal pet project where I'll be using...
14
by: mr_semantics | last post by:
I have been reading about the practise of casting values to unsigned char while using the <ctype.h> functions. For example, c = toupper ((unsigned char) c); Now I understand that the standard...
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
74
by: aruna.mysore | last post by:
Hi all, I have a simple definitioin in a C file something like this. main() { char a; ....... int k; }
17
by: spasmous | last post by:
I need a way to search through a block of memory for a char array "DA" using a pointer to a short. Ideally I would like to write something like: short *data = ... some data...; int j = 0;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.