Re: Hexadecimal numbers in Eu
- Posted by Derek Parnell <ddparnell at bigpond.com> Sep 19, 2004
- 429 views
Juergen Luethje wrote: > > Robert Craig wrote: > > > Juergen Luethje wrote: > >> > >> Derek Parnell wrote: > >> > >>> Juergen Luethje wrote: > >>>> > >>>> Hi, > >>>> > >>>> in some other programming languages, hex numbers can represent positive > >>>> or negative values. E.g. in PowerBASIC 3.2, > >>>> ? &hFFF0 ' prints -16 > >>>> ? &h00F0 ' prints 240 > >>>> > >>>>> From what I read at 'refman_2.htm#1' (Eu 2.4), and from my experience, > >>>> I have the impression that hexadecimal numbers (that don't have a '-' > >>>> sign at the beginning) in Euphoria _always_ have positive values. > >>>> > >>>> Rob, is this true? > >>>> > >>>> In Euphoria, e.g. > >>>> }}} <eucode> > >>>> ? #FFF0 -- prints 65520 > >>>> ? #00F0 -- prints 240 > >>>> </eucode> {{{ > >>> > >>> > >>> Yes, hexadecimal literals are always positive unless they have a leading > >>> minus symbol. > >> > >> Rob, maybe this sentence could be added to the docs? I think it will be > >> very helpful for beginners -- and people like me, who sometimes feel > >> like beginners. > > > > OK, I'll say something in the docs about this. > > Thanks, that will eliminate ambiguity. > > >>> However it is a bit confusing because negative integers, when converted > >>> to display as hex digits via the printf() routine, display without > >>> the minus sign. > >>> > >>> printf(1, "%x", -1) --> FFFFFFFF > >> > >> Yes, this actually confused me some days ago!! Thanks, Derek! > >> > >> Rob, is there any chance that in a future version of Euphoria, > >> printf(1, "%x", -1) > >> will show '-1' or '-#1' instead of 'FFFFFFFF'? > > > > Euphoria's %x format for printf() uses C's %x format, > > and only works for numbers up to 32-bits in size. > > }}} <eucode> > > printf(1, "%x", #FFFFFFFF) > > </eucode> {{{ > > displays FFFFFFFF > > as does: > > }}} <eucode> > > printf(1, "%x", -1) > > </eucode> {{{ > > but Euphoria does *not* consider #FFFFFFFF to be equal to -1. > > Either it should do so, or > }}} <eucode> > printf(1, "%x", -1) > </eucode> {{{ > should *not* display FFFFFFFF!! > This is a great inconsistency, and a source for confusion (as Derek > wrote, too) and bugs. > > > When people are printing numbers with %x format > > they usually want to see -1 displayed as FFFFFFFF, > > It depends. E.g. people who are new to programming probably would not > expect anything, because it will be the first time they deal with hex > numbers, and they just learn what they see. I'll interject just here because we probably should understand the purpose of %x. My understanding is it is used to display the bit pattern of the integer, as it is represented in RAM. And thus -1 should be output as FFFFFFFF because that is how -1 is represented as a 32-bit value. I agree it is confusing if you don't know that's what %x is trying to do. > I agree that more experienced programmers, printing numbers with %x > format, probably want to see -1 displayed as FFFFFFFF. But this people > probably also want Euphoria to consider #FFFFFFFF to be equal to -1! The difference between #FFFFFFFF and the output of %x is that the first is a literal coded into the source code, and such literals are always positive integers. And the second is bit pattern display of the integer in RAM. If you want to use negative hexadecimal literals you just place a '-' in front of the literal. atom test test = -#1 test = -#FFFF If you want to display the value (rather than the bit pattern) of an integer in hexadecimal format try this ... if test < 0 then printf(1, "-%x, ", -test) else printf(1, "%x, ", test) end if -- Derek Parnell Melbourne, Australia