Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.misc > #13393

Re: how much computers can do in one second

From Marko Rauhamaa <marko@pacujo.net>
Newsgroups comp.misc
Subject Re: how much computers can do in one second
Date 2017-03-30 19:09 +0300
Organization A noiseless patient Spider
Message-ID <87d1cywxtg.fsf@elektro.pacujo.net> (permalink)
References (1 earlier) <87r31iudou.fsf@elektro.pacujo.net> <87vaqsvr4i.fsf@bsb.me.uk> <8737dwwmdc.fsf@LkoBDZeT.terraraq.uk> <87r31gmn9w.fsf@elektro.pacujo.net> <cj7qdcdjcafrd88ena4k3no13d2bkuqutk@4ax.com>

Show all headers | View raw


Caecilius <nospam@spamless.invalid>:

> On Wed, 29 Mar 2017 12:43:07 +0300, Marko Rauhamaa <marko@pacujo.net>
> wrote:
>>Mental note: never do signed-integer arithmetics in C. Do your
>>calculations unsigned and just cast the final result to a signed type.
>
> I've found the opposite: it's easy to get tripped up by unsigned
> arithmetic in C because of the wraparound behaviour for negative
> numbers. I've been caught out by this once or twice when I was sure
> the end result would always be positive, but an intermediate result
> was negative which made the final result a huge and completely
> incorrect answer.

I think a C programmer needs to be very comfortable with 2's-complement
arithmetics.

In a nutshell, addition, subtraction and multiplication are well-behaved
under 2's-complement arithmetics. IOW, there's no difference between
signed and unsigned operations. (Division is another story.)

A C programmer also needs to regularly depend on the benign wrapping
behavior of (unsigned) integers. For example, you can safely implement a
ring buffer by incrementing the read index and the write index as long
as the size of the ring buffer is a power of 2. Similarly, you can
derive the number of open connections from the ever-increasing open and
close counts with a simple subtraction.


Marko

Back to comp.misc | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

how much computers can do in one second RS Wood <rsw@therandymon.com> - 2017-03-27 14:43 +0300
  Re: how much computers can do in one second Marko Rauhamaa <marko@pacujo.net> - 2017-03-27 15:06 +0300
    Re: how much computers can do in one second Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-29 01:55 +0100
      Re: how much computers can do in one second Marko Rauhamaa <marko@pacujo.net> - 2017-03-29 10:25 +0300
      Re: how much computers can do in one second Richard Kettlewell <invalid@invalid.invalid> - 2017-03-29 08:52 +0100
        Re: how much computers can do in one second Marko Rauhamaa <marko@pacujo.net> - 2017-03-29 12:43 +0300
          Re: how much computers can do in one second Caecilius <nospam@spamless.invalid> - 2017-03-30 16:07 +0100
            Re: how much computers can do in one second Marko Rauhamaa <marko@pacujo.net> - 2017-03-30 19:09 +0300
              Re: how much computers can do in one second Bob Eager <news0006@eager.cx> - 2017-03-30 21:36 +0000
                Re: how much computers can do in one second Marko Rauhamaa <marko@pacujo.net> - 2017-03-31 09:56 +0300
  Re: how much computers can do in one second Huge <Huge@nowhere.much.invalid> - 2017-03-27 13:51 +0000
  Re: how much computers can do in one second Sylvia Else <sylvia@not.at.this.address> - 2017-03-28 11:57 +1100
    Re: how much computers can do in one second Huge <Huge@nowhere.much.invalid> - 2017-03-28 09:51 +0000
      Re: how much computers can do in one second Bob Eager <news0006@eager.cx> - 2017-03-28 11:12 +0000

csiph-web