Suppose I have the following statement:
unsigned char x = 0;
If I do,
printf("%u\", ~x);
it prints the value of UINT_MAX. I am using Intel machine. This same
result is printed in both VC++ and gcc.
In the second edition of K&R, section A7.4.6 in page 204 says the
following:
"If the operand is unsigned, the result is computed by subracting the
value from the largest value of the promoted type".
Does it mean that since the operand is unsigned, the promoted type is
also unsigned. 6 4681
On 28 Dec 2006 21:09:42 -0800, "subramania n"
<su************ **@yahoo.comwro te:
>Suppose I have the following statement:
unsigned char x = 0;
If I do,
printf("%u\" , ~x);
it prints the value of UINT_MAX. I am using Intel machine. This same result is printed in both VC++ and gcc.
In the second edition of K&R, section A7.4.6 in page 204 says the following: "If the operand is unsigned, the result is computed by subracting the value from the largest value of the promoted type".
Does it mean that since the operand is unsigned, the promoted type is also unsigned.
x is promoted to unsigned int before applying operator ~. Thus the
result is the one's complement of 0u.
To print the unsigned char that is the one's complement of x, you
should use:
printf("%u\", (unsigned char)(~x));
That, is you should demote it before printing
Zara
Zara wrote:
On 28 Dec 2006 21:09:42 -0800, "subramania n"
<su************ **@yahoo.comwro te:
Suppose I have the following statement:
unsigned char x = 0;
If I do,
printf("%u\", ~x);
it prints the value of UINT_MAX. I am using Intel machine. This same
result is printed in both VC++ and gcc.
In the second edition of K&R, section A7.4.6 in page 204 says the
following:
"If the operand is unsigned, the result is computed by subracting the
value from the largest value of the promoted type".
Does it mean that since the operand is unsigned, the promoted type is
also unsigned.
x is promoted to unsigned int before applying operator ~.
No, x is promoted to /signed/ int. It is only promoted to unsigned int
if UCHAR_MAX INT_MAX, which is not the case on any machine capable of
running VC++ I'm aware of.
Thus the
result is the one's complement of 0u.
The ~ operator returns the bitwise complement of its operand. One's
complement refers to a particular representation of integers. (I know
you just copied it from the original message.)
To print the unsigned char that is the one's complement of x, you
should use:
printf("%u\", (unsigned char)(~x));
That, is you should demote it before printing
There is a possibility that ~0 is a trap representation. In this case,
it is better to print (unsigned char) ~(unsigned int) x instead, which
is guaranteed to be safe.
On 28 Dec 2006 22:29:19 -0800, "Harald van D?k" <tr*****@gmail. com>
wrote:
>Zara wrote:
>On 28 Dec 2006 21:09:42 -0800, "subramania n" <su*********** ***@yahoo.comwr ote:
>Suppose I have the following statement:
unsigned char x = 0;
If I do,
printf("%u\" , ~x);
<...>
>> x is promoted to unsigned int before applying operator ~.
No, x is promoted to /signed/ int. It is only promoted to unsigned int if UCHAR_MAX INT_MAX, which is not the case on any machine capable of running VC++ I'm aware of.
Yes, of course, you are right. (STD C-6.3.1.1, 2)
>
>Thus the result is the one's complement of 0u.
The ~ operator returns the bitwise complement of its operand. One's complement refers to a particular representation of integers. (I know you just copied it from the original message.)
You are right again.
>
>To print the unsigned char that is the one's complement of x, you should use:
printf("%u\" , (unsigned char)(~x));
That, is you should demote it before printing
There is a possibility that ~0 is a trap representation. In this case, it is better to print (unsigned char) ~(unsigned int) x instead, which is guaranteed to be safe.
Right again. Anyway, I would prefer to write
(unsigned char) (~(unsigned int) x)
Best regards,
zara
On Fri, 29 Dec 2006 06:43:33 +0100, Zara <me*****@dea.sp amcon.org>
wrote in comp.lang.c:
On 28 Dec 2006 21:09:42 -0800, "subramania n"
<su************ **@yahoo.comwro te:
Suppose I have the following statement:
unsigned char x = 0;
If I do,
printf("%u\", ~x);
it prints the value of UINT_MAX. I am using Intel machine. This same
result is printed in both VC++ and gcc.
In the second edition of K&R, section A7.4.6 in page 204 says the
following:
"If the operand is unsigned, the result is computed by subracting the
value from the largest value of the promoted type".
Does it mean that since the operand is unsigned, the promoted type is
also unsigned.
x is promoted to unsigned int before applying operator ~. Thus the
result is the one's complement of 0u.
That is quite incorrect. 'x' is promoted to signed int, since signed
into on this platform can hold all values of unsigned char.
--
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.l earn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
unsigned char x = 0;
What is the behaviour of ~x according to the C standard.
After reading K & R, I thought the variable will be promoted to signed
int. Taking bitwise complement should give INT_MAX. But it does not
happen here. I get UINT_MAX.
Can someone clarify my doubt ?
subramanian wrote:
unsigned char x = 0;
What is the behaviour of ~x according to the C standard.
Strictly speaking it is undefined on the virtual C machine.
>
After reading K & R, I thought the variable will be promoted to signed
int.
An unsigned char will be promoted to int if an int can hold all the
values
of unsigned char, otherwise it will be promoted to unsigned int. On
hosted implementations , it will promote to int.
Taking bitwise complement should give INT_MAX.
No, taking the bitwise complement of all bits zero yields all bits one.
There
are three different possible representations for signed integers,[1]
two's
complement, ones' complement and sign-magnitude. The value yielded is
different for each form of representation. Indeed, the 'value' may be a
trap
representation on a ones' complement machine.
[1] Unlike C99, C90 does not specify those representations explicitly,
though Committee members have commented that documents from the
'Normative References' component of C90 imply that those 3 options
are the only ones available to a C90 implementation.
But it does not happen here. I get UINT_MAX.
You snipped the bit of code you used to get that value. You previously
wrote...
printf("%u\", ~x);
Though what you probably meant to post was...
printf("%u\n", ~x);
That code invokes undefined behaviour because %u expects an unsigned
int and you supply a value of type int that is not representable as an
unsigned int (on a 2c machine.)[2]
Try...
printf("%d\n", ~x);
This will yield -1 on a 2c machine.
All that said, bitwise operations should generally only be performed on
unsigned integer types of rank equal or higher than unsigned int. Why?
To avoid the sorts of issues of undefined behaviour that you've
encountered.
[2] Strictly speaking, passing an int for %u invokes undefined
behaviour
even if int value is non-negative.
>
Can someone clarify my doubt ?
Read the texts very carefully. When working on character types, and
especially when working on bitwise operators, be very careful about
implicit promotions (e.g. integral promotions, usual arithmetic
promotions,
etc...).
--
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mantorok Redgormor |
last post by:
The only adder I was able to come up with for incrementing
by one, uses a for loop.
I was trying to do this without using a for loop
while emulating i++(it's for obfuscated code)
Anyone know of a way to increment by one continuously,
while updating the same value stored in an object,
without the need of such a loop? basically a one-liner.
|
by: Harshan |
last post by:
The range of signed int is - 2^15 to + (2^15 )-1 (-32768 to 32767) Why
the one less at positive Range ? (compared to the negative side..!)
I came to know that it uses 2's compliment internally for storing the
negative numbers . why it uses 2's compliment but not the ones compliment
for storing the negative numbers ?
|
by: Ruffin Bailey |
last post by:
I coulda sworn I was given an explanation during an AppDev class years
ago for VB6, but don't recall the answer. Why is it that -1 is True
in Visual Basic (and now VB.NET)? Bit flags seem like they should
always be 0 or 1 to me... (not that I haven't used VB long enough by
now to know better).
Sorry to pester, but "why is -1 = true?" is a difficult thing to
Google!
Ruffin Bailey
|
by: omisols |
last post by:
Gurus -
Can anyone explain the behavior here?
#include <stdio.h>
#include <stdlib.h>
int main ()
{
unsigned char c = 0;
|
by: subramanian |
last post by:
Consider the following code:
#include<stdio.h>
int main(void)
{
unsigned char c = 0;
unsigned int i = ~c;
| |
by: Dan Henry |
last post by:
I need a sanity check. The following is an exchange on
comp.arch.embedded with CBFalconer in a rather long MISRA related
thread. Since my little section of that thread and area of interest
was never really about MISRA, but rather the one's complement
representation of integer constant 0 and now how to zero memory
portably, I am bringing the topic to this group. Try to ignore CBF's
errors in the 1's complement -1 and sign magnitude -1...
|
by: Bob Greschke |
last post by:
I'm reading 3-byte numbers from a file and they are signed (+8 to
-8million). This seems to work, but I'm not sure it's right.
# Convert the 3-characters into a number.
Value1, Value2, Value3 = unpack(">BBB", Buffer)
Value = (Value1*65536)+(Value2*256)+Value3
if Value >= 0x800000:
Value -= 0x1000000
print Value
|
by: deepak |
last post by:
Hi,
when I execute following program which find's one's compliment of 2,
I'm getting -3 where
I was expecting -2. Is there anything fundamentally wrong in my
understanding?
main()
{
|
by: .rhavin grobert |
last post by:
guess you have a processor that can handle 32bit natively and you have
a 32-bit-int. im now looking for some *ultrafast* way to determine if
an int has more than one bit set. any ideas?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |