1. "atomic" constants

Hi, me again.
It looks like developping a wrapper is a good way to learn Euphoria with
some level of depth...

Now that I've mastered a little the data access to structures (thanks
David), I have a question on constants:

I'd like to have all constants predefined in this way (assuming all them are
32 bit):

constant IN_CLASSC_NET       =    #FFFFFF00
constant IN_CLASSC_NSHIFT    =    8
constant IN_CLASSC_HOST      =    #000000FF


constant INADDR_LOOPBACK     =    #7F000001
constant INADDR_NONE         =    #FFFFFFFF

I supposed that this would be adequate, but I've found that if I do a
printout of INADDR_NONE inside the debugger, I get 4.29497e+009, instead of
the "-1" that was intended, so the comparisons never match (and I'm getting
progressively insane)....

Shouldn't I use constants in this way?.
Thanks.
Jesus-winsock-is-closer.

new topic     » topic index » view message » categorize

2. Re: "atomic" constants

Jesus writes:
> constant INADDR_NONE         =    #FFFFFFFF
> I supposed that this would be adequate, but I've found that if I
> do a printout of INADDR_NONE inside the debugger, I get
> 4.29497e+009, instead of the "-1" that was intended, so the
> comparisons never match (and I'm getting progressively insane)....

Euphoria interprets #FFFFFFFF as a large positive number.
If your intention is to poke4 the number into memory then

    poke4(addr, #FFFFFFFF)

has the same effect as:

    poke4(addr, -1)

i.e. you will have 4 consecutive bytes in memory that contain
all 1-bits.

When you read 4 bytes from the same address,
you must choose whether to use peek4u (unsigned)
or peek4s (signed). peek4u(address) will return
#FFFFFFFF as a large positive atom, while peek4s
will return -1.

And always keep in mind that the Euphoria integer type
can only accept numbers in the range of roughly: minus one billion
(-1.07e9) to plus one billion (+1.07e9). That's equivalent to
31 bits, which is (inconveniently) 1 bit short of what you'd like
when manipulating 32-bit addresses. So declare anything that
might hold an address as "atom". (the integer type is
only 31 bits for performance reasons.)

Also keep in mind that Euphoria's "?" operator, and the
trace facility will both switch to scientific notation for large
numbers. It doesn't mean that you've lost any precision
on your numbers. If you want to see the full precision, you
have to use printf().

     printf(1, "%d", #FFFFFFFF)
or:
     printf(1, "%x", #FFFFFFFF)

will give you all the digits.

> For the sake of continuity, I've just put
>   constant INADDR_NONE         =    -1 -- #FFFFFFFF
> and everything works fine...

That might be a perfectly reasonable solution.
depending on what you are doing.

Regards,
     Rob Craig
     Rapid Deployment Software

new topic     » goto parent     » topic index » view message » categorize

3. Re: "atomic" constants

i can hear my asm lang teacher echoing in my head:
"always remember you have one less bit which
is used as the sign bit"
7bits not 8
15bits not 16
31bits not 32
ARGH
i can hear him all the way down the hall laughing from his office
"you forgot the sign bit again didn't you?"


Robert Craig wrote:
> And always keep in mind that the Euphoria integer type
> can only accept numbers in the range of roughly: minus one billion
> (-1.07e9) to plus one billion (+1.07e9). That's equivalent to
******
> 31 bits,
****** ARGH
> which is (inconveniently) 1 bit short of what you'd like
> when manipulating 32-bit addresses. So declare anything that
> might hold an address as "atom". (the integer type is
> only 31 bits for performance reasons.)

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu