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


Groups > comp.lang.c > #142874

Re: Application Exercises

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Application Exercises
Date 2019-04-06 23:46 +0100
Organization A noiseless patient Spider
Message-ID <87r2aeg3be.fsf@bsb.me.uk> (permalink)
References <exercise-20190406184435@ram.dialup.fu-berlin.de>

Show all headers | View raw


ram@zedat.fu-berlin.de (Stefan Ram) writes:

>   Disclaimer: I might have posted about the same programming
>   problem some years ago. But then it was my own invention,
>   while now I read about it in a book.
>
>   In a book about the job application as a programmer, I found
>   a paragraph, were they say that application exercises often
>   enforce arbitrary restrictions, which do not make sense
>   according to the author of this book, but the exercises have
>   to be done nevertheless. As an example he gave:
>
> |Write a function to detect whether two integral numbers are
> |equal without using any comparison operators.
>
>   . As a hint he wrote: "Think of bit operators.".

I think that's a bad hint.  It points towards thinking about
representations and not values, which looks to me to be the rockier
road.

I'm thinking along these lines:

 #define CMP(a, b) !((unsigned)(a) - (unsigned)(b))

 CMP(a/2, b/2) && CMP(a - a/2, b - b/2)

The reason you can't just do

  !((unsigned)a - (unsigned)b)

is that unsigned need only have the same range as the non-negative range
of int.  You can do a little better with

  !((uintmax_t)a - (uintmax_t)b)

but there would still be odd implementations where that could fail.

-- 
Ben.

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


Thread

Re: Application Exercises Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-04-06 23:46 +0100
  Re: Application Exercises Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-04-07 00:08 -0700
    Re: Application Exercises Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-04-08 01:24 +0100

csiph-web