473,608 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A one's complement sanity check, please

I need a sanity check. The following is an exchange on
comp.arch.embed ded 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 representations .
My questions are about 1's complement representation of the integer
constant 0. The exchange:

[exchange]
Dan Henry wrote:
CBFalconer <cb********@yah oo.comwrote:
>Dan Henry wrote:
>>CBFalconer <cb********@yah oo.comwrote:
Dan Henry wrote:
CBFalcone r <cb********@yah oo.comwrote:
>
... snip ...
>>>>>Wrong. C representation may be sign/magnitude or 1's complement.
>>
>2's comp: -1 ----0x0001 ---0xffff
> ~ 1 ---~0x0001 ---0xffff (note NOT sign)
~ 0 ---~0x0000 ---0xffff "
>1's comp: -1 ----0x0001 ---0xffff
> ~ 1 ---~0x0001 ---0xfffe (note NOT sign)
~ 0 ---~0x0000 ---0xffff "
or ~ 0 ---~0xffff ---0x0000 "
>sign mag: -1 ----0x0001 ---0xffff
> ~ 1 ---~0x0001 ---0xfffe (note NOT sign)
~ 0 ---~0x0000 ---0xffff "
>>
>because of the rules for bringing unsigned into range.
>
~1? I read Marcin to be asking whether ~0 won't set all 1s.

See the additions above. Only -1 always returns 0xffff.

Isn't the integer constant 0 (lacking the unary - operator) in a
1's complement system positive zero, not negative zero? Doesn't
your second ~0 example actually represent ~-0?

In 1's complement -0 is equal to +0.

Right, but I am wondering about the predictability of the
underlying bit patterns.

Are you saying that in 1's complement the integer constant 0 could
have the bit pattern for +0 (all 0's) *or* the bit pattern for -0
(all 1's) and that using the unary - operator on the integer
constant 0 can yield either all 1's or all 0's -- that my
memset(&object, 0, sizeof object) is equally likely to zero all
bits as set all bits of the memory that object occupies?
Exactly.
[/exchange]

The implied conclusion is that memset(&object, 0, sizeof object) may
not zero a chunk of memory on a one's complement system. I'm having
an extremely difficult time accepting that. I don't interpret
6.2.6.2p2 to allow for (normal) 0 to have any nonzero bits in its
one's complement representation. I have never seen purportedly
portable code that does anything special with memset's second argument
to zero a chunk of memory. When using integer constant 0 as the
second argument to memset, I don't see how any of the operators listed
in 6.2.6.2p3 could possibly come into play to have changed normal 0
into negative 0. If integer constant 0 does in fact yield a nonzero
bit pattern, then the one portable way I can see to pass an all-zeroes
bit pattern as memset's second argument is:

#define ZERO (0^0)
memset(&object, ZERO, sizeof object);

But I don't see that in purportedly portable code. Or could my (0^0)
also be flawed as the above quoted exchange implies, because the first
0 in ZERO's expansion could be represented by all zeroes and the
second 0 in the expansion could be represented by all ones (or vice
versa). Wouldn't the translator stick to one representation or the
other and not flip willy-nilly? Or is it that the XOR operator being
in 6.2.6.2p3 means that (0^0) could yield negative zero?

Color me confused.

--
Dan Henry
Apr 28 '07 #1
6 3392
Dan Henry wrote:
I need a sanity check.
[...]
[exchange]
Dan Henry wrote:
[...]
Are you saying that in 1's complement the integer constant 0 could
have the bit pattern for +0 (all 0's) *or* the bit pattern for -0
(all 1's) and that using the unary - operator on the integer
constant 0 can yield either all 1's or all 0's -- that my
memset(&object, 0, sizeof object) is equally likely to zero all
bits as set all bits of the memory that object occupies?

Exactly.
[/exchange]

The implied conclusion is that memset(&object, 0, sizeof object) may
not zero a chunk of memory on a one's complement system. I'm having
an extremely difficult time accepting that.
And rightly so, for two different reasons. Firstly, memset converts
its second operand to an unsigned char. Even if plain 0 were allowed
to be negative 0, it would be converted to all bits zero, since
unsigned char has no sign bit and no padding bits. Secondly, plain 0
is not allowed to be negative 0 in the first place, because negative
zero "shall be generated only by:
-- the &, |, ^, ~, <<, and >operators with arguments that produce
such a value;
-- the +, -, *, /, and % operators where one argument is a negative
zero and the result is zero;
-- compound assignment operators based on the above cases." (C99
6.2.6.2p3)

Apr 28 '07 #2
Dan Henry wrote:
>
I need a sanity check. The following is an exchange on
comp.arch.embed ded 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 representations .
My questions are about 1's complement representation of the integer
constant 0. The exchange:

[exchange]
Dan Henry wrote:
>CBFalconer <cb********@yah oo.comwrote:
>>Dan Henry wrote:
CBFalconer <cb********@yah oo.comwrote:
Dan Henry wrote:
>CBFalcon er <cb********@yah oo.comwrote:
>>
>... snip ...
>>>>>>Wrong. C representation may be sign/magnitude or 1's complement.
>>>
>>2's comp: -1 ----0x0001 ---0xffff
>> ~ 1 ---~0x0001 ---0xffff (note NOT sign)
~ 0 ---~0x0000 ---0xffff "
>>1's comp: -1 ----0x0001 ---0xffff
>> ~ 1 ---~0x0001 ---0xfffe (note NOT sign)
~ 0 ---~0x0000 ---0xffff "
or ~ 0 ---~0xffff ---0x0000 "
>>sign mag: -1 ----0x0001 ---0xffff
>> ~ 1 ---~0x0001 ---0xfffe (note NOT sign)
~ 0 ---~0x0000 ---0xffff "
>>>
>>because of the rules for bringing unsigned into range.
>>
>~1? I read Marcin to be asking whether ~0 won't set all 1s.
>
See the additions above. Only -1 always returns 0xffff.

Isn't the integer constant 0 (lacking the unary - operator) in a
1's complement system positive zero, not negative zero? Doesn't
your second ~0 example actually represent ~-0?

In 1's complement -0 is equal to +0.

Right, but I am wondering about the predictability of the
underlying bit patterns.

Are you saying that in 1's complement the integer constant 0 could
have the bit pattern for +0 (all 0's) *or* the bit pattern for -0
(all 1's) and that using the unary - operator on the integer
constant 0 can yield either all 1's or all 0's -- that my
memset(&object , 0, sizeof object) is equally likely to zero all
bits as set all bits of the memory that object occupies?

Exactly.
[/exchange]

The implied conclusion is that memset(&object, 0, sizeof object)
may not zero a chunk of memory on a one's complement system. I'm
having an extremely difficult time accepting that. I don't
interpret 6.2.6.2p2 to allow for (normal) 0 to have any nonzero
bits in its one's complement representation. I have never seen
purportedly portable code that does anything special with memset's
second argument to zero a chunk of memory. When using integer
constant 0 as the second argument to memset, I don't see how any
of the operators listed in 6.2.6.2p3 could possibly come into play
to have changed normal 0 into negative 0. If integer constant 0
does in fact yield a nonzero bit pattern, then the one portable
way I can see to pass an all-zeroes bit pattern as memset's second
argument is:
>From N869:
7.21.6.1 The memset function

Synopsis

[#1]

#include <string.h>
void *memset(void *s, int c, size_t n);

Description

[#2] The memset function copies the value of c (converted to
an unsigned char) into each of the first n characters of the
object pointed to by s.

Returns

[#3] The memset function returns the value of s.
>
#define ZERO (0^0)
memset(&object, ZERO, sizeof object);

But I don't see that in purportedly portable code. Or could my (0^0)
also be flawed as the above quoted exchange implies, because the first
0 in ZERO's expansion could be represented by all zeroes and the
second 0 in the expansion could be represented by all ones (or vice
versa). Wouldn't the translator stick to one representation or the
other and not flip willy-nilly? Or is it that the XOR operator being
in 6.2.6.2p3 means that (0^0) could yield negative zero?
Apart from the above I am going to stay out of this on this group.
At least for now. :-)

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

Apr 28 '07 #3
Harald van Dijk wrote:
Dan Henry wrote:
I need a sanity check.
[...]
[exchange]
Dan Henry wrote:
[...]
Are you saying that in 1's complement the integer constant 0 could
have the bit pattern for +0 (all 0's) *or* the bit pattern for -0
(all 1's) and that using the unary - operator on the integer
constant 0 can yield either all 1's or all 0's -- that my
memset(&object, 0, sizeof object) is equally likely to zero all
bits as set all bits of the memory that object occupies?
Exactly.
[/exchange]

The implied conclusion is that memset(&object, 0, sizeof object) may
not zero a chunk of memory on a one's complement system. I'm having
an extremely difficult time accepting that.

And rightly so, for two different reasons. Firstly, memset converts
its second operand to an unsigned char. Even if plain 0 were allowed
to be negative 0, it would be converted to all bits zero, since
unsigned char has no sign bit and no padding bits. Secondly, plain 0
is not allowed to be negative 0 in the first place, because negative
zero "shall be generated only by:
-- the &, |, ^, ~, <<, and >operators with arguments that produce
such a value;
-- the +, -, *, /, and % operators where one argument is a negative
zero and the result is zero;
-- compound assignment operators based on the above cases." (C99
6.2.6.2p3)
Sorry, after rereading your message, I see you are already aware of
this paragraph. In that case, I really don't even see why there might
be doubts.

Apr 28 '07 #4
On 28 Apr 2007 11:42:25 -0700, Harald van D?k <tr*****@gmail. com>
wrote:
>Harald van D?k wrote:
>Dan Henry wrote:
I need a sanity check.
[...]
[exchange]
Dan Henry wrote:
[...]
Are you saying that in 1's complement the integer constant 0 could
have the bit pattern for +0 (all 0's) *or* the bit pattern for -0
(all 1's) and that using the unary - operator on the integer
constant 0 can yield either all 1's or all 0's -- that my
memset(&object, 0, sizeof object) is equally likely to zero all
bits as set all bits of the memory that object occupies?

Exactly.
[/exchange]

The implied conclusion is that memset(&object, 0, sizeof object) may
not zero a chunk of memory on a one's complement system. I'm having
an extremely difficult time accepting that.

And rightly so, for two different reasons. Firstly, memset converts
its second operand to an unsigned char. Even if plain 0 were allowed
to be negative 0, it would be converted to all bits zero, since
unsigned char has no sign bit and no padding bits. Secondly, plain 0
is not allowed to be negative 0 in the first place, because negative
zero "shall be generated only by:
-- the &, |, ^, ~, <<, and >operators with arguments that produce
such a value;
-- the +, -, *, /, and % operators where one argument is a negative
zero and the result is zero;
-- compound assignment operators based on the above cases." (C99
6.2.6.2p3)

Sorry, after rereading your message, I see you are already aware of
this paragraph. In that case, I really don't even see why there might
be doubts.
Doubts because although I am an extremely infrequent poster here, I am
a regular reader and use this newsgroup as an ongoing test of my
knowledge of the language even though I've been using the language
since the early '80s. When a regular c.l.c. poster, one seemingly
held in high regard, has a different viewpoint or interpretation of
the language, I doubt *my* knowledge first and try to research on my
own. This time after my research, I wasn't able to come to peace with
the discussion, so am seeking additional guidance.

--
Dan Henry
Apr 28 '07 #5
On Apr 29, 6:38 am, Harald van Dijk <true...@gmail. comwrote:
Secondly, plain 0 is not allowed to be negative 0 in the first place,
because negative zero "shall be generated only by:
-- the &, |, ^, ~, <<, and >operators with arguments that produce
such a value;
-- the +, -, *, /, and % operators where one argument is a negative
zero and the result is zero;
-- compound assignment operators based on the above cases." (C99
6.2.6.2p3)
Is (0^0) allowed to produce a negative zero? (It would be funny
if the original code were correct but the suggested 'fix' were wrong).
Apr 29 '07 #6
Old Wolf wrote:
On Apr 29, 6:38 am, Harald van Dijk <true...@gmail. comwrote:
Secondly, plain 0 is not allowed to be negative 0 in the first place,
because negative zero "shall be generated only by:
-- the &, |, ^, ~, <<, and >operators with arguments that produce
such a value;
-- the +, -, *, /, and % operators where one argument is a negative
zero and the result is zero;
-- compound assignment operators based on the above cases." (C99
6.2.6.2p3)

Is (0^0) allowed to produce a negative zero? (It would be funny
if the original code were correct but the suggested 'fix' were wrong).
"With arguments that produce such a value" means that -1^1 is allowed
to produce negative zero, not 0^0. 0 is all (non-padding) bits zero,
so 0^0 is all bits zero too, and all bits zero represents positive
zero.

Apr 30 '07 #7

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

Similar topics

4
52450
by: Abby | last post by:
I have an array which contain Hex no. in each position. For examples, unsigned char data; data = 0x00; data = 0x01; data = 0x02; data = 0xE; data = 0xEF; --> This is the checksum value
19
2412
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 ?
26
21310
by: Paul | last post by:
public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
33
2493
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
2
2886
by: Jonathan Pryor | last post by:
I'm in need of a sanity check: is the following code valid C++? namespace foo { class NamespaceClass { }; } using namespace foo; class Bug_GlobalFriendDeclaresNamespaceClass {
22
979
by: sarathy | last post by:
Hi all, I have a few doubts in the 1's and 2's complement representation. Generally negative numbers can be represented using either 1's complement or 2's complement representation. 1's complement ---reverse all the bits 2's complement ---reverse all the bits + 1 i.e 1's complement of 2 ( 0000 0010 ) is -2 ( 1111 1101 ) But when a number and its complement are added the result must be a
3
7556
by: vijaybaskar3108 | last post by:
hi, I just want to know how to find complements for a number. These are the following answers for complements 2's complement(10110)=01010 4's complement(1230)=2110 5's complement(4322)=0123 please tell me how to do it.how this answers comes.This is a global edge question. please also tell me how to find 8's complement(7436) please alos tell me how to find 3's,4's,5's,6's,7's,8's complements.
10
4888
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() {
32
9632
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?
0
8501
Oralloy
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...
0
8349
tracyyun
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...
0
6820
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, 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...
1
6015
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 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...
0
5479
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();...
0
3967
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...
1
2477
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
1
1607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1336
bsmnconsultancy
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...

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.