delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/09/04/08:00:06

From: info AT hoekstra-uitgeverij DOT nl (Richard Bos)
Newsgroups: comp.lang.c,comp.os.msdos.djgpp
Subject: Re: hex calculation routine!?
Date: Mon, 04 Sep 2000 11:15:31 GMT
Organization: Go wash your mouth.
Lines: 36
Message-ID: <39b38189.258387457@news.worldonline.nl>
References: <p2m2rsg34070tpo2vvia7bdlugv6h3g107 AT 4ax DOT com> <nns4rssto1eudtvktejqkhjkee3vdtje5e AT 4ax DOT com>
NNTP-Posting-Host: vp199-55.worldonline.nl
X-Trace: nereid.worldonline.nl 968066023 2923 195.241.199.55 (4 Sep 2000 11:13:43 GMT)
X-Complaints-To: newsmaster AT worldonline DOT nl
NNTP-Posting-Date: 4 Sep 2000 11:13:43 GMT
X-Newsreader: Forte Free Agent 1.21/32.243
Path: news.mv.net!newspeer.phoen-x.net!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!colt.net!newspeer.clara.net!news.clara.net!news-x.support.nl!news.worldonline.nl!not-for-mail
Xref: news.mv.net comp.lang.c:420649 comp.os.msdos.djgpp:103153

Damian Yerrick <Bullcr_pd_yerrick AT hotmail DOT comRemoveBullcr_p> wrote:

> Posted and emailed to ^Hawk^ <Hawk AT DarkSoft DOT Net>:
> 
> >Has anybody a routine to add/subtract etc. hex-values for me? Thnx in
> >advance! =;)
> >
> >ie. 800F0F0F+8F0=??? or 800F0F0F-8F0=???.
> 
> /* assuming unsigned long is a 32-bit or larger data type */

It always is in ISO C; ULONG_MAX is at least 2**32-1.

> unsigned long foo = 0x800f0f0f
> unsigned long bar = 0x8f0
> 
> printf("%lx - %lx = %lx\n", foo, bar, foo - bar);
> 
> IIRC, %x prints an int as hexadecimal, and %lx prints a long as
> hexadecimal.  Crossposted to comp.lang.c for further comment on this
> portable language issue.

Yup. Note also that in C, you have hex constants (in the source), but no
hex _values_. You have integer values; by the time printf() or any other
function gets to deal with them, representation is irrelevant. You could
just as easily do this:

  printf("%ld - %ld = %lx\n", foo, bar, foo - bar);
  printf("%lx - %lx = %ld\n", foo, bar, foo - bar);

(%ld specifies long decimal output)

or any other combination of decimal or hex output. Ditto for values
originally specified as decimal constants.

Richard

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019