473,412 Members | 2,599 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,412 software developers and data experts.

Quick check on signed promotion of bytes

toe

I have a byte called "x". I want byte "y" to be the complement of
"x" (i.e. all the bits flipped).

Initially I wrote:

char unsigned x, y;

...

x = 72;

y = ~x;

But then I thought that the following might happen on your average
system (CHAR_BIT == 8, sizeof(int) == 4):

1) x is promoted to signed int.
2) The complement is take of this signed int.
3) This signed int is then converted to an unsigned char

Am I right in thinking that this is what will happen? If so, would I
be wise to do the following instead:

y = ~(unsigned)x;

--
Tomás Ó hÉilidhe
Jan 31 '08 #1
3 1431
On Jan 31, 12:45 pm, t...@lavabit.com wrote:
I have a byte called "x". I want byte "y" to be the complement of
"x" (i.e. all the bits flipped).

Initially I wrote:

char unsigned x, y;

...

x = 72;

y = ~x;

But then I thought that the following might happen on your average
system (CHAR_BIT == 8, sizeof(int) == 4):

1) x is promoted to signed int.
That won't happend, therefore the rest of your thoughts is false.
Your code is perfectly valid.
Jan 31 '08 #2
toe
On Jan 31, 10:48*am, vipps...@gmail.com wrote:
* * 1) x is promoted to signed int.

That won't happend, therefore the rest of your thoughts is false.
Your code is perfectly valid.
My understanding was as follows:

"If you're dealing with an integer type smaller than int, then it
must undergo promotion before you can perform any operations on it."

--
Tomás Ó hÉilidhe
Jan 31 '08 #3
On Thu, 31 Jan 2008 02:48:28 -0800 (PST), vi******@gmail.com wrote in
comp.lang.c:
On Jan 31, 12:45 pm, t...@lavabit.com wrote:
I have a byte called "x". I want byte "y" to be the complement of
"x" (i.e. all the bits flipped).

Initially I wrote:

char unsigned x, y;

...

x = 72;

y = ~x;

But then I thought that the following might happen on your average
system (CHAR_BIT == 8, sizeof(int) == 4):

1) x is promoted to signed int.
That won't happend, therefore the rest of your thoughts is false.
Your code is perfectly valid.
No, your correction is false. 'x' must be promoted to either signed
or unsigned int before the bitwise inversion operator is applies. In
a system with CHAR_BIT is 8, a signed int must be able to hold all
possible values of unsigned char, so 'x' is indeed promoted to signed
int.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Feb 1 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Fred Ma | last post by:
Hello, I've been trying to clear up a confusion about integer promotions during expression evaluation. I've checked the C FAQ and C++ FAQ (they are different languages, but I was hoping one...
9
by: dam_fool_2003 | last post by:
For int data type the default range starts from signed to unsigned. If we don't want negative value we can force an unsigned value. The same goes for long also. But I don't understand why we have...
10
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post by:
Hello everybody, this is my first post to a newsgroup at all. I would like to get some feedback on one proposal I am thinking about: --- begin of proposal --- Proposal to add...
6
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but...
39
by: Juha Nieminen | last post by:
I was once taught that if some integral value can never have negative values, it's a good style to use an 'unsigned' type for that: It's informative, self-documenting, and you are not wasting half...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.