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


Groups > comp.lang.c > #34923

Re: 64-bit integers where the implementation supports max 32-bit ints

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: 64-bit integers where the implementation supports max 32-bit ints
Date 2013-08-05 12:24 -0700
Organization None to speak of
Message-ID <lnwqo0rldz.fsf@nuthaus.mib.org> (permalink)
References <ktolnp$lkb$1@dont-email.me>

Show all headers | View raw


"James Harris" <james.harris.1@gmail.com> writes:
> On a 16-bit C compiler which supports up to 32-bit integers but no larger 
> how feasible is it to support 64-bit integer values and is there a good way 
> to do so? Best I can think of is to pass structs around principally because, 
> AIUI, they can be returned from functions. But is there a better way?

Since 1999, the ISO C standard has required the type "long long" to be
at least 64 bits wide.  Strictly speaking, if you're using a compiler
that doesn't support "long long", then you're not using a conforming C
compiler.  To know how to work around that, you'd have to know what
other language features it doesn't support (such as, say, passing
structs by value).

If you're working with a compiler that conforms reasonably well to C90,
which didn't require long long or any 64-bit integer type, then you do
have a lot more

> Such a struct would be along the lines of
>
>   struct custom64 {
>     uint32_t low;
>     uint32_t high;
>   };
>   typedef struct custom64 custom64_t;
>
> (For the 64-bit compiler this would instead be "typedef uint64_t 
> custom64_t;".)

If you use "typedef uint64_t custom64_t;" for compilers that support
64-bit integers, it will be easy to write code that assumes custom64_t
is an integer type, which will break on your non-64-bit platform.  You
might be better off defining it as a struct with a single uint64_t
member, and writing alternative functions/macros to perform operations
on them.

The name "uint32_t" was added by the same standard (C99) that mandated
the existence of "long long".  Do you have a C90 compiler that supports
uint32_t as an extension?  If not, you might need to define uint32_t
yourself.

If you care about the representation of your custom64_t mapping onto a
64-bit integer, endianness is going to be an issue.  You might want to
use an #ifdef to control the order of the "low' and "high" members.  If
the representation is never stored in a file or transmitted, that
probably doesn't matter.

[...]

> BTW, I should say I'm sure there are extensive libraries for wide number 
> manipulation but they are not what I want. Something short and simple that 
> will fit in a few lines would be much preferable to something pre-written 
> and extensive.
>
> Am I on the right lines? Is there a 'standard' way to do stuff  like this in 
> C?

Yes, I think you're on the right track.  If you have 32-bit unsigned
integers but not 64-bit unsigned integers, a struct like you've defined
is a good way to emulate them.  Implementing the operations you need is,
of course, left as an exercise.  And probably you don't need to
implement *all* the operations; if you never divide 64-bit integers, you
don't have to implement 64-bit division.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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


Thread

64-bit integers where the implementation supports max 32-bit ints "James Harris" <james.harris.1@gmail.com> - 2013-08-05 18:07 +0100
  Re: 64-bit integers where the implementation supports max 32-bit ints Shao Miller <sha0.miller@gmail.com> - 2013-08-05 13:16 -0400
  Re: 64-bit integers where the implementation supports max 32-bit ints glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-08-05 18:57 +0000
  Re: 64-bit integers where the implementation supports max 32-bit ints Keith Thompson <kst-u@mib.org> - 2013-08-05 12:24 -0700
    Re: 64-bit integers where the implementation supports max 32-bit ints "James Harris" <james.harris.1@gmail.com> - 2013-08-05 22:37 +0100
      Re: 64-bit integers where the implementation supports max 32-bit ints Robert Wessel <robertwessel2@yahoo.com> - 2013-08-05 20:20 -0500
        Re: 64-bit integers where the implementation supports max 32-bit ints Robert Wessel <robertwessel2@yahoo.com> - 2013-08-05 23:34 -0500
      Re: 64-bit integers where the implementation supports max 32-bit ints Keith Thompson <kst-u@mib.org> - 2013-08-05 18:42 -0700
        Re: 64-bit integers where the implementation supports max 32-bit ints "James Harris" <james.harris.1@gmail.com> - 2013-08-06 11:15 +0100
          Re: 64-bit integers where the implementation supports max 32-bit ints James Kuyper <jameskuyper@verizon.net> - 2013-08-06 08:00 -0400
            Re: 64-bit integers where the implementation supports max 32-bit ints "James Harris" <james.harris.1@gmail.com> - 2013-08-06 13:59 +0100
              Re: 64-bit integers where the implementation supports max 32-bit ints James Kuyper <jameskuyper@verizon.net> - 2013-08-06 09:31 -0400
                Re: 64-bit integers where the implementation supports max 32-bit ints "James Harris" <james.harris.1@gmail.com> - 2013-08-06 15:12 +0100
                Re: 64-bit integers where the implementation supports max 32-bit ints Malcolm McLean <malcolm.mclean5@btinternet.com> - 2013-08-06 07:19 -0700
                Re: 64-bit integers where the implementation supports max 32-bit ints Keith Thompson <kst-u@mib.org> - 2013-08-06 08:43 -0700
                Re: 64-bit integers where the implementation supports max 32-bit ints Ian Collins <ian-news@hotmail.com> - 2013-08-07 15:51 +1200
                Re: 64-bit integers where the implementation supports max 32-bit ints Stephen Sprunk <stephen@sprunk.org> - 2013-08-06 21:48 -0500
                Re: 64-bit integers where the implementation supports max 32-bit ints "James Harris" <james.harris.1@gmail.com> - 2013-08-07 11:11 +0100
                Re: 64-bit integers where the implementation supports max 32-bit ints Stephen Sprunk <stephen@sprunk.org> - 2013-08-07 05:46 -0500
                Re: 64-bit integers where the implementation supports max 32-bit ints Rosario1903 <Rosario@invalid.invalid> - 2013-08-07 17:38 +0200
                Re: 64-bit integers where the implementation supports max 32-bit ints Keith Thompson <kst-u@mib.org> - 2013-08-07 09:27 -0700
                Re: 64-bit integers where the implementation supports max 32-bit ints Rosario1903 <Rosario@invalid.invalid> - 2013-08-07 18:36 +0200
                Re: 64-bit integers where the implementation supports max 32-bit ints Keith Thompson <kst-u@mib.org> - 2013-08-07 10:04 -0700
                Re: 64-bit integers where the implementation supports max 32-bit ints Rosario1903 <Rosario@invalid.invalid> - 2013-08-07 19:49 +0200
                Re: 64-bit integers where the implementation supports max 32-bit ints Keith Thompson <kst-u@mib.org> - 2013-08-07 11:13 -0700
                Re: 64-bit integers where the implementation supports max 32-bit ints Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2013-08-07 12:37 -0600
                Re: 64-bit integers where the implementation supports max 32-bit ints Rosario1903 <Rosario@invalid.invalid> - 2013-08-07 19:46 +0200
                Re: 64-bit integers where the implementation supports max 32-bit ints Stephen Sprunk <stephen@sprunk.org> - 2013-08-08 01:17 -0500
                Re: 64-bit integers where the implementation supports max 32-bit ints Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2013-08-08 09:54 +0300
                Re: 64-bit integers where the implementation supports max 32-bit ints Rosario1903 <Rosario@invalid.invalid> - 2013-08-08 16:38 +0200
                Re: 64-bit integers where the implementation supports max 32-bit ints Keith Thompson <kst-u@mib.org> - 2013-08-08 08:27 -0700
                Re: 64-bit integers where the implementation supports max 32-bit ints Keith Thompson <kst-u@mib.org> - 2013-08-07 09:05 -0700
                Re: 64-bit integers where the implementation supports max 32-bit ints Malcolm McLean <malcolm.mclean5@btinternet.com> - 2013-08-07 03:48 -0700
                Re: 64-bit integers where the implementation supports max 32-bit ints James Kuyper <jameskuyper@verizon.net> - 2013-08-06 11:55 -0400
                Re: 64-bit integers where the implementation supports max 32-bit ints Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-08-06 21:21 +0100
          Re: 64-bit integers where the implementation supports max 32-bit ints Keith Thompson <kst-u@mib.org> - 2013-08-06 08:38 -0700
        Re: 64-bit integers where the implementation supports max 32-bit ints "James Harris" <james.harris.1@gmail.com> - 2013-08-06 12:15 +0100
        Re: 64-bit integers where the implementation supports max 32-bit ints Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-08-06 09:36 -0400
          Re: 64-bit integers where the implementation supports max 32-bit ints Tim Rentsch <txr@alumni.caltech.edu> - 2013-08-08 14:18 -0700
      Re: 64-bit integers where the implementation supports max 32-bit ints Malcolm McLean <malcolm.mclean5@btinternet.com> - 2013-08-06 05:30 -0700

csiph-web