4

it's idiomatic to initialize a block of memory to zero by

memset(p, 0, size_of_p);

when we want to initialize it to minus one, we can:

memset(p, -1, size_of_p);

no matter what type p is, because in two's complemenatry representation, minus one is 0xff for 8 bits integer, 0xffff for 16 bits, and 0xffffffff for 32 bits.

My concern is, is such two's complementary representation universally applicable in the realm of modern computers? Can I expect such code platform-independent and robust enough to port to other platforms?

Thanks in advance.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Kun Wu
  • 329
  • 2
  • 8
  • Your question title and your question *body* are related, but a little-off-congruent. Your title seems to ask if bitwise two's-compliment is universally equivalent (it better be, since it is just a bit operation on a sequence of bits). But your question body falls into the presentation of negative values (specifically `-1`) and whether *that* is universally platform independent at the per-byte level of the overlaying type. It is not. So the answer to your question-title is *yes*; your question-body is *no*. (as I read it anyway, but I think it may be too late for me to be reading code). – WhozCraig Feb 05 '13 at 07:02
  • @WhozCraig I've changed the topic :D – Kun Wu Feb 05 '13 at 07:21

2 Answers2

4

No, there are three schemes of representing negative numbers allowed by the ISO C standard:

  • two's complement;
  • ones' complement; and
  • sign/magnitude.

However, you should keep in mind that it's been a long time since I've seen a platform using the two less common schemes. I would say that all modern implementations use two's complement.

You only want to concern yourself with this if you're trying to be 100% portable. If you're the type of person who's happy to be 99.99999% portable, don't worry about it.


See also info on the ones' complement Unisys 2200 (still active as at 2010) and this answer explaining the layouts.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • In that case it's practically a yes. :) – Alexey Frunze Feb 05 '13 at 06:32
  • If, say, I just want to initialize a bunch of numbers to be negative (all valid values are nonnegative), it seems the three schemes you mentioned all apply. Am I right? – Kun Wu Feb 05 '13 at 06:36
  • @Kevin, see my comment on 100 vs 99.99999%. If you want that extra 0.00001% coverage, you'll need to set each individual array element to -1. But do you really want to expend all that effort for the sake of three computers in Upper Wherevia? :-) – paxdiablo Feb 05 '13 at 06:38
3

The simple answer is yes, but the better answer is that you're prematurely optimising.

Write code that is obvious instead of code that you think is fast:

  for(i = 0; i < p; i++)
    array[i] = -1;

Will be automatically converted by an optimising compiler to the fastest possible representation (in VS it will become a memset when p is large enough) that does what you want without you having to think about whether this premature optimisation is always valid.

SecurityMatt
  • 6,593
  • 1
  • 22
  • 28