SmallInteger.st
changeset 5496 338c81a04468
parent 5458 2aa89d4fad64
child 5782 dcc037e5bdbf
equal deleted inserted replaced
5495:d42dbc9ac39d 5496:338c81a04468
   745              defined in the language standard (since the implementation
   745              defined in the language standard (since the implementation
   746              is free to choose any internal representation for integers)"
   746              is free to choose any internal representation for integers)"
   747 
   747 
   748     |mask|
   748     |mask|
   749 
   749 
   750     (index between:1 and:SmallInteger maxBits) ifFalse:[
   750     index <= 0 ifTrue:[
   751         ^ SubscriptOutOfBoundsSignal 
   751         ^ SubscriptOutOfBoundsSignal 
   752                 raiseRequestWith:index
   752                 raiseRequestWith:index
   753                 errorString:'index out of bounds'
   753                 errorString:'index out of bounds'
   754     ].
   754     ].
       
   755     (index > SmallInteger maxBits) ifTrue:[^ 0].
   755     mask := 1 bitShift:(index - 1).
   756     mask := 1 bitShift:(index - 1).
   756     ((self bitAnd:mask) == 0) ifTrue:[^ 0].
   757     ((self bitAnd:mask) == 0) ifTrue:[^ 0].
   757     ^ 1
   758     ^ 1
       
   759 
       
   760     "
       
   761      16r00000001 bitAt:0
       
   762      16r00000001 bitAt:1 
       
   763      16r00008000 bitAt:16 
       
   764      16r00800000 bitAt:24 
       
   765      16r08000000 bitAt:28 
       
   766      16r10000000 bitAt:29
       
   767      16r20000000 bitAt:30 
       
   768      16r40000000 bitAt:31 
       
   769      16r80000000 bitAt:32 
       
   770      16r100000000 bitAt:33 
       
   771     "
   758 !
   772 !
   759 
   773 
   760 bitClear:anInteger
   774 bitClear:anInteger
   761     "return the bitwise-and of the receiver and the complement of the argument, anInteger,
   775     "return the bitwise-and of the receiver and the complement of the argument, anInteger,
   762      returning the receiver with bits of the argument cleared."
   776      returning the receiver with bits of the argument cleared."
   792 
   806 
   793 %{  /* NOCONTEXT */
   807 %{  /* NOCONTEXT */
   794 
   808 
   795     /* oring the tags doesn't change it */
   809     /* oring the tags doesn't change it */
   796     if (__isSmallInteger(anInteger)) {
   810     if (__isSmallInteger(anInteger)) {
   797 	RETURN ( ((OBJ) ((INT)self | (INT)anInteger)) );
   811         RETURN ( ((OBJ) ((INT)self | (INT)anInteger)) );
   798     }
   812     }
   799 %}.
   813 %}.
   800     ^ self retry:#bitOr: coercing:anInteger
   814     ^ self retry:#bitOr: coercing:anInteger
   801 
   815 
   802     "(2r000000100 bitOr:2r00000011) radixPrintStringRadix:2"
   816     "
       
   817      (2r000000100 bitOr:2r00000011) radixPrintStringRadix:2
       
   818      (0 bitOr:16r20000000) hexPrintString   
       
   819      (0 bitOr:16r40000000) hexPrintString
       
   820      (0 bitOr:16r80000000) hexPrintString
       
   821     "
   803 !
   822 !
   804 
   823 
   805 bitShift32:shiftCount
   824 bitShift32:shiftCount
   806     "return the value of the receiver shifted by shiftCount bits,
   825     "return the value of the receiver shifted by shiftCount bits,
   807      but only within 32 bits, shifting into/out-of the sign bit.
   826      but only within 32 bits, shifting into/out-of the sign bit.
   956 %{  /* NOCONTEXT */
   975 %{  /* NOCONTEXT */
   957 
   976 
   958     if (__isSmallInteger(anInteger)) {
   977     if (__isSmallInteger(anInteger)) {
   959         int index = __intVal(anInteger);
   978         int index = __intVal(anInteger);
   960 
   979 
       
   980         if (index > 0) {
   961 #ifdef alpha64
   981 #ifdef alpha64
   962         if (index <= 63)
   982             if (index <= 62)
   963 #else
   983 #else
   964         if (index <= 30)
   984             if (index <= 30)
   965 #endif
   985 #endif
   966         {
   986             {
   967             INT mask = __MASKSMALLINT(1 << (index-1));
   987                 INT mask = __MASKSMALLINT(1 << (index-1));
   968 
   988 
   969             RETURN ( ((OBJ) ((INT)self & ~(INT)mask)) );
   989                 RETURN ( ((OBJ) ((INT)self & ~(INT)mask)) );
       
   990             }
       
   991             RETURN (self);  /* nothing to do ... */
   970         }
   992         }
   971         RETURN (self);
   993     }
   972     }
   994 %}.
   973 %}.
   995     ^ super clearBit:anInteger
   974     ^ self retry:#clearBit: coercing:anInteger
       
   975 
   996 
   976     "
   997     "
   977      (16r401 clearBit:1     ) hexPrintString
   998      (16r401 clearBit:1     ) hexPrintString
       
   999      (16r401 clearBit:0     ) hexPrintString
   978      (16r3fffffff clearBit:1) hexPrintString
  1000      (16r3fffffff clearBit:1) hexPrintString
   979      (16r3fffffff clearBit:29) hexPrintString 
  1001      (16r3fffffff clearBit:29) hexPrintString  
   980      (16r3fffffff clearBit:30) hexPrintString 
  1002      (16r3fffffff clearBit:30) hexPrintString  
   981      (16r3fffffff clearBit:31) hexPrintString  
  1003      (16r3fffffff clearBit:31) hexPrintString  
   982      (16r3fffffff bitAt:30) hexPrintString    
  1004      (16r3fffffff bitAt:29) hexPrintString     
   983      (16r3fffffff bitAt:29) hexPrintString  
  1005      (16r3fffffff bitAt:30) hexPrintString     
       
  1006      (16r3fffffff bitAt:31) hexPrintString       
   984      (16r40000001 clearBit:1) hexPrintString  
  1007      (16r40000001 clearBit:1) hexPrintString  
   985      (16rF0000001 clearBit:29) hexPrintString
  1008      (16rF0000001 clearBit:29) hexPrintString   
   986      (16rF0000001 clearBit:30) hexPrintString
  1009      (16rF0000001 clearBit:30) hexPrintString
   987      (16rF0000001 clearBit:31) hexPrintString 
  1010      (16rF0000001 clearBit:31) hexPrintString 
   988      (16rF0000001 clearBit:32) hexPrintString 
  1011      (16rF0000001 clearBit:32) hexPrintString 
   989     "
  1012     "
   990 
  1013 
  1186      SmallInteger maxVal highBit 
  1209      SmallInteger maxVal highBit 
  1187     "
  1210     "
  1188 
  1211 
  1189 !
  1212 !
  1190 
  1213 
       
  1214 invertBit:anInteger
       
  1215     "return a new number where the specified bit is inverted.
       
  1216      Bits are counted from 1 starting with the least significant.
       
  1217      The methods name may be missleading: the receiver is not changed,
       
  1218      but a new number is returned. Should be named #withBitInverted:"
       
  1219 
       
  1220 %{  /* NOCONTEXT */
       
  1221 
       
  1222     if (__isSmallInteger(anInteger)) {
       
  1223         int index = __intVal(anInteger);
       
  1224 
       
  1225         if (index > 0) {
       
  1226 #ifdef alpha64
       
  1227             if (index <= 62)
       
  1228 #else
       
  1229             if (index <= 30)
       
  1230 #endif
       
  1231             {
       
  1232                 INT mask = __MASKSMALLINT(1 << (index-1));
       
  1233 
       
  1234                 RETURN ( ((OBJ) ((INT)self ^ (INT)mask)) );
       
  1235             }
       
  1236         }
       
  1237     }
       
  1238 %}.
       
  1239     ^ super invertBit:anInteger
       
  1240 
       
  1241     "
       
  1242      (16r401 invertBit:2     ) hexPrintString      
       
  1243      (16r401 invertBit:1     ) hexPrintString      
       
  1244      (16r30000000 invertBit:1) hexPrintString     
       
  1245      (16r40000000 invertBit:0) hexPrintString  
       
  1246      (16r0 invertBit:29) hexPrintString             
       
  1247      (16r0 invertBit:30) hexPrintString             
       
  1248      (16r0 invertBit:31) hexPrintString             
       
  1249      (16r0 invertBit:32) hexPrintString       
       
  1250      (16r0 invertBit:33) hexPrintString       
       
  1251      (16r0 invertBit:100) hexPrintString      
       
  1252     "
       
  1253 
       
  1254 
       
  1255 !
       
  1256 
  1191 lowBit
  1257 lowBit
  1192     "return the bitIndex of the lowest bit set. The returned bitIndex
  1258     "return the bitIndex of the lowest bit set. The returned bitIndex
  1193      starts at 1 for the least significant bit. Returns -1 if no bit is set."
  1259      starts at 1 for the least significant bit. Returns -1 if no bit is set."
  1194 
  1260 
  1195 %{  /* NOCONTEXT */
  1261 %{  /* NOCONTEXT */
  1284 
  1350 
  1285 setBit:anInteger
  1351 setBit:anInteger
  1286     "return a new number where the specified bit is on.
  1352     "return a new number where the specified bit is on.
  1287      Bits are counted from 1 starting with the least significant.
  1353      Bits are counted from 1 starting with the least significant.
  1288      The methods name may be missleading: the receiver is not changed,
  1354      The methods name may be missleading: the receiver is not changed,
  1289      but a new number is returned. Should be named #withBitCleared:"
  1355      but a new number is returned. Should be named #withBitSet:"
  1290 
  1356 
  1291 %{  /* NOCONTEXT */
  1357 %{  /* NOCONTEXT */
  1292 
  1358 
  1293     if (__isSmallInteger(anInteger)) {
  1359     if (__isSmallInteger(anInteger)) {
  1294         int index = __intVal(anInteger);
  1360         int index = __intVal(anInteger);
  1295 
  1361 
       
  1362         if (index > 0) {
  1296 #ifdef alpha64
  1363 #ifdef alpha64
  1297         if (index <= 63)
  1364             if (index <= 62)
  1298 #else
  1365 #else
  1299         if (index <= 30)
  1366             if (index <= 30)
  1300 #endif
  1367 #endif
  1301         {
  1368             {
  1302             INT mask = __MASKSMALLINT(1 << (index-1));
  1369                 INT mask = __MASKSMALLINT(1 << (index-1));
  1303 
  1370 
  1304             RETURN ( ((OBJ) ((INT)self | (INT)mask)) );
  1371                 RETURN ( ((OBJ) ((INT)self | (INT)mask)) );
       
  1372             }
  1305         }
  1373         }
  1306         RETURN (self);
  1374     }
  1307     }
  1375 %}.
  1308 %}.
  1376     ^ super setBit:anInteger
  1309     ^ self retry:#setBit: coercing:anInteger
       
  1310 
  1377 
  1311     "
  1378     "
  1312      (16r401 setBit:2     ) hexPrintString      
  1379      (16r401 setBit:2     ) hexPrintString      
  1313      (16r30000000 setBit:1) hexPrintString     
  1380      (16r30000000 setBit:1) hexPrintString     
  1314      (16r40000000 setBit:0) hexPrintString  
  1381      (16r40000000 setBit:0) hexPrintString  
  3187 ! !
  3254 ! !
  3188 
  3255 
  3189 !SmallInteger class methodsFor:'documentation'!
  3256 !SmallInteger class methodsFor:'documentation'!
  3190 
  3257 
  3191 version
  3258 version
  3192     ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.131 2000-07-11 13:41:44 cg Exp $'
  3259     ^ '$Header: /cvs/stx/stx/libbasic/SmallInteger.st,v 1.132 2000-08-03 17:48:44 cg Exp $'
  3193 ! !
  3260 ! !