[M3devel] 32bit negative base 16 constants?

Jay K jay.krell at cornell.edu
Thu Apr 11 08:17:51 CEST 2013


I want a 32bit signed value expressed in base 16 where e.g. 16_FFFFFFFF is equivalent to -1.

 - Jay

From: hosking at cs.purdue.edu
Date: Thu, 11 Apr 2013 16:03:02 +1000
To: jay.krell at cornell.edu
CC: m3devel at elegosoft.com
Subject: Re: [M3devel] 32bit negative base 16 constants?



Actually, writing that you get what you expressed:
If an explicit base is present, the value of the literal must be less than 2^Word.Size, and its interpretation uses the convention of the Word interface.

Word.T is unsigned, so you only get the 32 bits you asked for.
If you want a signed value then you need to express the sign explicitly starting from a constant, just as you have done with:
CONST UID_WORD = (* 16_97E237E2 *) -1881139893;

On Apr 11, 2013, at 3:14 PM, Jay K <jay.krell at cornell.edu> wrote:  
  I have some 32bit constants.   
  
e.g.:  
  
 CONST UID_WORD = 16_97E237E2;  
  
 but this isn't right. 
 On a 64bit system, that is a 64bit integer 
 with a "large" positive value, instead of 
 the intended 32bit negative value. 
  
 Is there an ideal (i.e. CONST) way to do this? 
 Portably to 64bit systems? 
  
 I have found two unsatisfactory choices: 
  
 1) use decimal: 
   
 #include <stdio.h> 
 int main() 
 { 
 printf("%X %d\n", 0x97E237E2); 
 return 0; 
 } 
 
 
 cc 1.c 
 ./a.out 
97E237E2 -1881139893

CONST UID_WORD = (* 16_97E237E2 *) -1881139893;
 
 
 2) change it to VAR and a runtime conversion:
 
 
 PROCEDURE SignExtend(a, b: INTEGER): INTEGER =
 BEGIN
    b := Word.LeftShift(-1, b - 1); 
    IF Word.And(a, b) # 0 THEN 
        a := Word.Or(a, b); 
    END; 
    RETURN a; 
 END SignExtend; 

 PROCEDURE SignExtend32(a: INTEGER): INT32 = 
 BEGIN 
     RETURN SignExtend(a, 32); 
 END SignExtend32; 

 CONST IntegerToTypeid = SignExtend32; 


 VAR UID_WORD := IntegerToTypeid(16_97E237E2); (* CARDINAL *) 

This gives a warning:

CONST UID_INTEGER : INT32 = 16_195C2A74; (* INTEGER *)

and then at runtime gives an error assigning it to an INT32.


 any other ways? 


Thanks,
 - Jay






Antony Hosking | Associate Professor | Computer Science | Purdue University305 N. University Street | West Lafayette | IN 47907 | USAMobile +1 765 427 5484


 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20130411/94639418/attachment-0002.html>


More information about the M3devel mailing list