A signed int reserves one bit to signify whether a number is positive or
negative. In light of this, a colleague asked me whether there existed an
int in C++ that was -0, a zero with the negative bit set. I was intrigued
by this, so I tried the following code:
#include <stdio.h>
int main(int, char**) {
int a(-0);
printf("a=%d\n" , a);
if ( a==0 ) printf("a==0\n" );
if ( a==-0 ) printf("a==-0\n");
return 0;
}
The output is:
a=0
a==0
a==-0
So it seems that no such number can be used in C++, and speaking of
mathematical numbers, it makes perfect sense, but what is actually going on
"behing the curtains" in C++ since the compiled code can't discern between
+0 and -0 ?
--
Christian Stigen Larsen -- http://sublevel3.org/~csl/
Jul 19 '05
20 7562
"Suzanne Vogel" <su************ *@hotmail.com> wrote... Thanks for the feedback.
(e.g., from -0 to 0?
Why do you think that would be a type conversion? Anyway, the compiler may keep the bits as they are and just treat 0 and -0 as equal.
*Suppose* (this is hypothetical) there were different representations of 0 and -0 for longs but not for ints.
Consider the following (made-up example):
if (0 == -0)... // possibly convert one of these
To compare 0 and -0, they must have the same type. So in *this* made-up case, -0 would be a long
No, it wouldn't. See the explanation below.
and 0 would be an int, so 0 would be converted (converted, not cast) to a long.
Similarly to the following (real example):
if (float(0.0) == double(0.0))... // convert float(0.0) to a double
** Is this not right?
No. Not according to the C++ grammar. In it the code "-0" is
not a literal but an expression. "0" is a literal. It has the
type 'int' and base 8 (because it begins with 0). It's an octal
literal, and since it can be represented by 'int', it is an 'int'.
Now, -0 is a constant expression which involves negation operator
and the integer expression. The result is calculated by the
compiler on the fly and is (you guessed it!) 0. const int NUM_BITS_PER_CH AR = 8;
Better use std::numeric_li mits<char>::dig its from the <limits> header or CHAR_BIT from <climits>.
Good idea.
** Doesn't that just give the number of base-10 digits?
No. For those there is std::numeric_li mits<char>::dig its10.
If so, I guess I could take the log base-2 of that to get the number of base-2 digits.
You don't need to.
Victor
"Suzanne Vogel" <su************ *@hotmail.com> wrote in message
news:3f******** **@news.unc.edu ... For readers who skim, I'll start with a question (instead of burying it below): *** Is there a C++ library function that prints binary representations of numbers?
In a way, yes. You can ask C++ to convert the number to a string in binary
format and then print that string:
int a; // the variable you wish to represent in binary
format.
char* string = new char[50];
itoa(a, string, 2); // the 2 means binary
std::cout << string;
delete string;
"MiniDisc_2 k2" <Ma******@nospa m.com> wrote... "Suzanne Vogel" <su************ *@hotmail.com> wrote in message news:3f******** **@news.unc.edu ... For readers who skim, I'll start with a question (instead of burying it below): *** Is there a C++ library function that prints binary representations of numbers? In a way, yes. You can ask C++ to convert the number to a string in binary format and then print that string:
int a; // the variable you wish to represent in binary format. char* string = new char[50]; itoa(a, string, 2); // the 2 means binary
Where did you dig out that function? Neither Standard C, nor
Standard C++ library has 'itoa'.
std::cout << string; delete string;
On Thu, 3 Jul 2003 13:38:13 +0000 (UTC), cs******@newsca che.ntnu.no
(Christian Stigen Larsen) wrote in comp.lang.c++: A signed int reserves one bit to signify whether a number is positive or negative. In light of this, a colleague asked me whether there existed an int in C++ that was -0, a zero with the negative bit set. I was intrigued by this, so I tried the following code:
#include <stdio.h>
int main(int, char**) { int a(-0); printf("a=%d\n" , a); if ( a==0 ) printf("a==0\n" ); if ( a==-0 ) printf("a==-0\n"); return 0; }
The output is:
a=0 a==0 a==-0
So it seems that no such number can be used in C++, and speaking of mathematical numbers, it makes perfect sense, but what is actually going on "behing the curtains" in C++ since the compiled code can't discern between +0 and -0 ?
There are three defined formats for the representation of negative
signed integer values. Two of them have the physical possibility of
representing -0, one does not. Of the two that can physically
represent -0, that might actually be a valid value, or it might be a
trap value that causes undefined behavior.
On implementations that support -0, if you could actually find one, it
is required to compare equal to ordinary 0.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
"Jack Klein" <ja*******@spam cop.net> wrote in message news:2c******** *************** *********@4ax.c om... There are three defined formats for the representation of negative signed integer values.
Only in C99.
"Ron Natalie" <ro*@sensor.com > wrote... "Jack Klein" <ja*******@spam cop.net> wrote in message
news:2c******** *************** *********@4ax.c om... There are three defined formats for the representation of negative signed integer values.
Only in C99.
What is that supposed to mean? The representation of negative
integers is not language-specific, it's hardware-specific.
"Victor Bazarov" <v.********@att Abi.com> wrote in message news:vg******** ****@corp.super news.com... "Ron Natalie" <ro*@sensor.com > wrote... "Jack Klein" <ja*******@spam cop.net> wrote in message
news:2c******** *************** *********@4ax.c om... There are three defined formats for the representation of negative signed integer values.
Only in C99.
What is that supposed to mean? The representation of negative integers is not language-specific, it's hardware-specific.
C99 defines (well limits the allowable) integer formats to three (signed mag, 1's comp.,
and 2's comp.). There is NO such restriction in C++. Any encoding that meets the
requirement that the positive numbers share representation with the corresponding unsigned
type meets the requirement.
"Ron Natalie" <ro*@sensor.com > wrote... "Victor Bazarov" <v.********@att Abi.com> wrote in message
news:vg******** ****@corp.super news.com... "Ron Natalie" <ro*@sensor.com > wrote... "Jack Klein" <ja*******@spam cop.net> wrote in message
news:2c******** *************** *********@4ax.c om... > There are three defined formats for the representation of negative > signed integer values.
Only in C99.
What is that supposed to mean? The representation of negative integers is not language-specific, it's hardware-specific. C99 defines (well limits the allowable) integer formats to three (signed
mag, 1's comp., and 2's comp.). There is NO such restriction in C++. Any encoding
that meets the requirement that the positive numbers share representation with the
corresponding unsigned type meets the requirement.
Would you say, then, that C99 is smarter about it or that C++
is more flexible about it? AFAIK, there are no other schemes
than the three mentioned in this thread...
Victor
"Victor Bazarov" <v.********@att Abi.com> wrote in message news:vg******** ****@corp.super news.com...
.. Would you say, then, that C99 is smarter about it or that C++ is more flexible about it? AFAIK, there are no other schemes than the three mentioned in this thread...
Got me. I've never understood why they were suddenly enumerated
in C99. To my knowledge I've never worked on a signed-mag machine
and I haven't been near a 1's complement machine in over a decade.
Ron Natalie wrote:
[...] Got me. I've never understood why they were suddenly enumerated in C99. To my knowledge I've never worked on a signed-mag machine and I haven't been near a 1's complement machine in over a decade.
How about offset binary (aka "Excess-K"), various BCD schemes, uhmm,
and "gray code"? ;-)
regards,
alexander. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: MiniDisc_2k2 |
last post by:
Okay, here's a question about the standard. What does it say about
unsigned/signed mismatches in a comparison statement:
char a = 3;
unsigned char b = 255;
if (a<b)
Now what's the real answer here? If a is converted to unsigned, then b>a.
But, if b is converted to signed,then a>b. What's the correct coversion
(what is the compiler supposed to do?)
|
by: Rade |
last post by:
Following a discussion on another thread here... I have tried to understand
what is actually standardized in C++ regarding the representing of integers
(signed and unsigned) and their conversions. The reference should be 3.9.1
(Fundamental types), and 4.7 (Integral conversions).
It seems to me that the Standard doesn't specify:
1) The "value representation" of any of these types, except that (3.9.1/3)
"... The range of nonnegative...
|
by: Christopher Dyken |
last post by:
Hi group,
I'm trying to implement two routines to handle 32x32-bits and
64x64-bits signed integer multiplication on a 32 bits machine in C. It
easy to find descriptions of non-signed multiplication, however, I
haven't found any good descriptions for the signed counterpart.
Does any of you have a good reference for this topic?
Thanks in advance,
|
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 signed char which is -256. Does it
means that we can assign the same ASCII value to both signed and
unsigned. That means the ASCII value can be represented with a type of
signed char and also unsigned char?
For example
int main(void)
{
|
by: tinesan |
last post by:
Hello fellow C programmers,
I'm just learning to program with C, and I'm wondering what the
difference between signed and unsigned char is. To me there seems to
be no difference, and the standard doesn't even care what a normal char
is (because signed and unsigned have equal behavior).
For example if someone does this:
unsigned char a = -2; /* or = 254 */
| |
by: juanitofoo |
last post by:
Hello,
I've just switched to gcc 4 and I came across a bunch of warnings that
I can't fix. Example:
#include <stdio.h>
int main()
{
signed char *p = "Hola";
|
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
signed/unsigned modifier to class declarations
to next revision of C++ programming language
|
by: Chris |
last post by:
How do we create digitally signed MDE files in Access 2007 ?
I tried to follow the procedure I was using in other office versions.
1. I installed the Verisign certificate (with private key).
2. Signed the MDB from VBA.
3. Create the MDE.
When I try to open the signed MDE, even on the developement machine
with
|
by: somenath |
last post by:
Hi All,
I am trying to undestand "Type Conversions" from K&R book.I am not
able to understand the
bellow mentioned text
"Conversion rules are more complicated when unsigned operands are
involved. The problem
is that comparisons between signed and unsigned values are machine-
dependent, because
they depend on the sizes of the various integer types. For example,
suppose that int is 16 bits
|
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 vaguely . Can
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |