Hi,
What is the difference between a signed 0x00 (NULL, or 0) and an
unsigned 0x00? Can there be one? If I do the following:
char var;
var = 0x00;
what should var hold? and if it was an unsigned char, what should it
hold then? Thanks
Sona 6 14123
Sona <so**********@n ospam.com> wrote: What is the difference between a signed 0x00 (NULL, or 0) and an unsigned 0x00? Can there be one?
First, let's get rid of all the misconceptions.
To begin with, 0x00 is always signed. It's a hexadecimal integral
constant with the value 0, unsuffixed. This means that it is an int, and
an int is signed.
You probably meant "an object with all bits zero". Because of the
previous point, 0x00 is not a good way to express this, but I'll assume
you meant this anyway for the following points.
The second point is that NULL is a null pointer constant, not a null
pointer; the difference is important. A null pointer constant is a part
of your source code, not an object. It can be 0, or (void*)0, or
('-'-'-'), or (void *)(1 - sizeof (char) ), or anything equivalent. It
has no bit pattern as such; but in a pointer context, it is converted to
a null pointer, which has.
A null pointer, then, is not guaranteed to have the bit pattern "all
zeroes". It often does; but this is not required. All that's required is
that it is a distinct pointer that does not point at any valid object.
Comparing a null pointer to 0 works because the 0, in this pointer
context, is converted to another null pointer, not because the null
pointer is in any way compared to an integral 0.
Third, it doesn't matter. Signed integers must use the same
representation for non-negative values as their unsigned counterparts;
and this must be a pure binary representation. Moreover, all comparisons
go by value, so if you compare 0x00 with 0x00u, they must compare equal.
If I do the following:
char var; var = 0x00;
what should var hold?
The value 0. An int with the value 0 is assigned to a char, and since
that char can hold this value, the result should be as expected.
Note that, since we don't know whether chars are signed or unsigned on
your system, we don't know whether it's a signed or unsigned zero,
either. Note also that, as above, it shouldn't matter a jot.
and if it was an unsigned char, what should it hold then?
The value 0, in an unsigned char object.
Richard
Thanks for that Richard. That cleared it :)
My problem is that I need to send hex values to an embedded device to
get a response back. The manual says that sending '0x00' should return
0x88, but that's not working. I know this isn't related to C, but this
might help you understand what my problem is:
I send 0x99 to the device to switch it to "listening mode". This works..
I simple do:
char msg[2];
msg[0] = 0x99;
SendToDevice(ua rt, msg, 1);
where uart is a pointer to the device, msg is the char array that it
takes in and 1 is the number of characters to read from the array. This
works fine, that is, it activates the device.
This returns the correct result. I then send it two values, say:
msg[0] = 0x45;
msg[1] = 0x32;
SendToDevice(ua rt, msg, 2);
This works as well, I get the correct checksum in return. But then I
need to "initiliaze " the device by sending it 0x00 and it's supposed to
return 0x66 which isn't working.. I'm doing:
msg[0] = 0x00;
SendToDevice(ua rt, msg, 1);
And I get -91 in return. I posted another post about the itoa and atoi
problem just now defining that problem.. maybe it's something that I'm
doing wrong with my conversion but it seems to work fine for the first
two cases but fails with 0x00, or maybe the manual is wrong.. I don't
know. I'm new at this and am trying to learn but I can't seem to get
anywhere because apparantly my C isn't up to the mark :) Thanks for the
help! I hope you can shed some light on this?
Sona
Richard Bos wrote: Sona <so**********@n ospam.com> wrote:
What is the difference between a signed 0x00 (NULL, or 0) and an unsigned 0x00? Can there be one?
First, let's get rid of all the misconceptions.
To begin with, 0x00 is always signed. It's a hexadecimal integral constant with the value 0, unsuffixed. This means that it is an int, and an int is signed. You probably meant "an object with all bits zero". Because of the previous point, 0x00 is not a good way to express this, but I'll assume you meant this anyway for the following points.
The second point is that NULL is a null pointer constant, not a null pointer; the difference is important. A null pointer constant is a part of your source code, not an object. It can be 0, or (void*)0, or ('-'-'-'), or (void *)(1 - sizeof (char) ), or anything equivalent. It has no bit pattern as such; but in a pointer context, it is converted to a null pointer, which has. A null pointer, then, is not guaranteed to have the bit pattern "all zeroes". It often does; but this is not required. All that's required is that it is a distinct pointer that does not point at any valid object. Comparing a null pointer to 0 works because the 0, in this pointer context, is converted to another null pointer, not because the null pointer is in any way compared to an integral 0.
Third, it doesn't matter. Signed integers must use the same representation for non-negative values as their unsigned counterparts; and this must be a pure binary representation. Moreover, all comparisons go by value, so if you compare 0x00 with 0x00u, they must compare equal.
If I do the following:
char var; var = 0x00;
what should var hold?
The value 0. An int with the value 0 is assigned to a char, and since that char can hold this value, the result should be as expected.
Note that, since we don't know whether chars are signed or unsigned on your system, we don't know whether it's a signed or unsigned zero, either. Note also that, as above, it shouldn't matter a jot.
and if it was an unsigned char, what should it hold then?
The value 0, in an unsigned char object.
Richard
In <3f********@cla rion.carno.net. au> Sona <so**********@n ospam.com> writes: What is the difference between a signed 0x00 (NULL, or 0) and an
NULL doesn't belong to this context. It may defined as (void *)0, which
is quite a different beast than 0x0 and 0.
unsigned 0x00? Can there be one? If I do the following:
char var; var = 0x00;
what should var hold? and if it was an unsigned char, what should it hold then? Thanks
In your example it doesn't matter what flavour of the 0 constant you're
using. There are contexts where it does matter, for example ~0u has a
well defined value, while ~0 is implementation-defined and can be even
a trap representation. But when used as an initialiser, the type of an
integer constant with the value 0 does not matter.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Sona <so**********@n ospam.com> writes: Hi,
What is the difference between a signed 0x00 (NULL, or 0) and an unsigned 0x00? Can there be one? If I do the following:
NULL is not necessarily the same thing as 0x00. It may be a pointer
type, and (if so) may not be represented as all-bits-zero. char var; var = 0x00;
what should var hold? and if it was an unsigned char, what should it hold then? Thanks
Always positive zero. Negative zero can only occur on implementations which
use ones complement or sign-and-magnitude notation to represent
negative numbers. Both of these representations are extremely rare--I
have not personally encountered one. two's complement notation is
simpler to handle (requires no "special" handling of negative
numbers).
Even if you have such a representation on your C implementation,
negative zero may only result from:
1) Bit manipulation
2) Arithmetic where one operand is negative zero and the result is zero.
And, simply obtaining a negative zero can be a trap representation,
resulting in undefined behavior.
All of this doesn't mean you can assume your code won't encounter a
negative 0, but this is only a problem when you are making assumptions
about the representation of a particular "zero", and didn't roll your
own (i.e., 0x00 or the like). The cases where you actually have to
specifically account for negative zero are pretty rare. I've never had
to: generally, being responsible with your bitwise operations means not
producing such a thing in the first place (especially since you should be using
unsigned integers with your bitwise operators). Any code that *could*
produce a negative zero is not portable, since (as pointed out
earlier), negative zero may be a trap representation.
HTH,
-Micah
Sona <so**********@n ospam.com> writes: msg[0] = 0x00; SendToDevice(ua rt, msg, 1);
And I get -91 in return. I posted another post about the itoa and atoi problem just now defining that problem.. maybe it's something that I'm doing wrong with my conversion but it seems to work fine for the first two cases but fails with 0x00, or maybe the manual is wrong.. I don't know. I'm new at this and am trying to learn but I can't seem to get anywhere because apparantly my C isn't up to the mark :) Thanks for the help! I hope you can shed some light on this?
This definitely has nothing to do with negative zeros. I'm afraid
that, as far as we can tell from what you have described, you are
sending correct data. Perhaps -91 is an error value?
-Micah
Sona <so**********@n ospam.com> writes: Thanks for that Richard. That cleared it :)
My problem is that I need to send hex values to an embedded device to get a response back. The manual says that sending '0x00' should return 0x88, but that's not working. I know this isn't related to C, but this might help you understand what my problem is:
[...]
You're not really sending hex values to your embedded device. You're
sending integer values (I'm guessing 8-bit unsigned integer values) to
the device; these values happen to be represented in hex in your C
program. (Unless the device is really expecting a sequence of ASCII
(or other character encoding) characters '0', 'x', '0', '0', but that
seems unlikely.) When you're programming on this level, it's
particularly important to understand the relationship between how
things are represented in your C source program (as character
sequences) and how they're represented in the actual hardware (as
bits, bytes, and words).
[...] But then I need to "initiliaze " the device by sending it 0x00 and it's supposed to return 0x66 which isn't working.. I'm doing:
msg[0] = 0x00; SendToDevice(ua rt, msg, 1);
And I get -91 in return. I posted another post about the itoa and atoi problem just now defining that problem.. maybe it's something that I'm doing wrong with my conversion but it seems to work fine for the first two cases but fails with 0x00, or maybe the manual is wrong.. I don't know. I'm new at this and am trying to learn but I can't seem to get anywhere because apparantly my C isn't up to the mark :) Thanks for the help! I hope you can shed some light on this?
How is SendToDevice declared? What type does it return? Your sample
code throws away the return value; how do you know it returns -91?
Assuming an 8-bit two's-complement result, -91, treated as an unsigned
8-bit value, is 0xa5. Does that make more sense?
--
Keith Thompson (The_Other_Keit h) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Siemel Naran |
last post by:
Hi. Is there a way to convert the type signed int to the type unsigned int,
char to unsigned char, signed char to unsigned char, and so on for all the
fundamental integer types? Something like
template <>
struct to_unsigned<signed int> : public std::unary_function<signed int,
unsigned int>
{
unsigned int operator()(signed int x) const { return x; }
};
|
by: Andy |
last post by:
Hi,
Are 1 through 4 defined behaviors in C?
unsigned short i;
unsigned long li; /* 32-bit wide */
1. i = 65535 + 3;
2. i = 1 - 3;
3. li = (unsigned long)0xFFFFFFFF + 3;
4. li = 1 - 3;
|
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: TTroy |
last post by:
Hello, I'm relatively new to C and have gone through more than 4 books
on it. None mentioned anything about integral promotion, arithmetic
conversion, value preserving and unsigned preserving. And K&R2
mentions "signed extension" everywhere.
Reading some old clc posts, I've beginning to realize that these books
are over-generalizing the topic. I am just wondering what the
difference between the following pairs of terms are:
1)...
|
by: Hanzac Chen |
last post by:
Hi,
I don't understand why this could happen?
The Code 1 will output `fff9'
and the Code 2 will output `1'
How could the `mod 8' not have effect?
/* Code 1 */
#include <stdio.h>
#include <stdlib.h>
| |
by: techie |
last post by:
I have defined a number of unsigned integer types as follows:
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedfe long long uint64;
Is it necessary to explicitly cast from one type of unsigned integer type to
another even though they do so implicitly?
|
by: Nicholas Zhou |
last post by:
Hi,
I was writing a testing program to test the ranges of char, short, int
and long variables on my computer, both signed and unsigned.
Everything was fine except for unsigned int and unsigned long. I got
0 to -1 for both. The expected answers should be:
unsigned int: 0 to 65535
unsigned long: 0 to 4294967295
|
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: 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: 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: 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: 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: 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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |