[M3devel] 32bit negative base 16 constants?

Jay K jay.krell at cornell.edu
Thu Apr 11 07:14:10 CEST 2013


  
  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


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


More information about the M3devel mailing list