#DOCUMENTATION
class: SmallInteger
category of:
#bitAt:
#clearBit:
#invertBit:
#setBit:
--- a/SmallInteger.st Wed Jan 20 02:45:31 2016 +0100
+++ b/SmallInteger.st Wed Jan 20 04:19:23 2016 +0100
@@ -984,61 +984,6 @@
"(2r001010100 bitAnd:2r00001111) radixPrintStringRadix:2"
!
-bitAt:anIntegerIndex
- "return the value of the index's bit (index starts at 1) as 0 or 1.
- Notice: the result of bitAt: on negative receivers is not
- defined in the language standard (since the implementation
- is free to choose any internal representation for integers)"
-
-%{ /* NOCONTEXT */
-#ifdef __SCHTEAM__
- return context._RETURN( self.bitAt(anIntegerIndex));
-#else
- if (__isSmallInteger(anIntegerIndex)) {
- INT idx = __smallIntegerVal(anIntegerIndex);
- if (idx > 0) {
- if (idx > N_INT_BITS) {
- RETURN(__mkSmallInteger(0));
- }
- RETURN((__smallIntegerVal(self) & ((INT)1 << (idx-1))) ? __mkSmallInteger(1) : __mkSmallInteger(0));
- }
- }
-#endif /* not __SCHTEAM__ */
-%}.
-
- ^ SubscriptOutOfBoundsError
- raiseRequestWith:anIntegerIndex
- errorString:'index out of bounds'
-
- "
- 16r000000001 bitAt:0 -> error
- 16r000000001 bitAt:1
- 16r000000001 bitAt:2
- 16r000008000 bitAt:16
- 16r000800000 bitAt:24
- 16r008000000 bitAt:28
- 16r010000000 bitAt:29
- 16r020000000 bitAt:30
- 16r040000000 bitAt:31
- 16r080000000 bitAt:32
- 16r100000000 bitAt:33
- "
-
-" Smalltalk implementation:
- |mask|
-
- anIntegerIndex <= 0 ifTrue:[
- ^ SubscriptOutOfBoundsSignal
- raiseRequestWith:anIntegerIndex
- errorString:'index out of bounds'
- ].
- (anIntegerIndex > SmallInteger maxBits) ifTrue:[^ 0].
- mask := 1 bitShift:(anIntegerIndex - 1).
- ((self bitAnd:mask) == 0) ifTrue:[^ 0].
- ^ 1
-"
-!
-
bitClear:anInteger
"return the bitwise-and of the receiver and the complement of the argument, anInteger,
returning the receiver with bits of the argument cleared.
@@ -1552,54 +1497,6 @@
^ self retry:#bitXor: coercing:anInteger
!
-clearBit:anInteger
- "return a new integer where the specified bit is off.
- Bits are counted from 1 starting with the least significant.
- The method's name may be misleading: the receiver is not changed,
- but a new number is returned. Should be named #withBitCleared:"
-
-%{ /* NOCONTEXT */
-#ifndef __SCHTEAM__
- if (__isSmallInteger(anInteger)) {
- int index = __intVal(anInteger);
-
- if (index > 0) {
-# if __POINTER_SIZE__ == 8
- if (index <= 62)
-# else
- if (index <= 30)
-# endif
- {
- INT mask = __MASKSMALLINT( ((INT)1 << (index-1)));
-
- RETURN ( ((OBJ) ((INT)self & ~(INT)mask)) );
- }
- RETURN (self); /* nothing to do ... */
- }
- }
-#endif /* not __SCHTEAM__ */
-%}.
- ^ super clearBit:anInteger
-
- "
- (16r401 clearBit:1 ) hexPrintString
- (16r401 clearBit:0 ) hexPrintString
- (16r3fffffff clearBit:1) hexPrintString
- (16r3fffffff clearBit:29) hexPrintString
- (16r3fffffff clearBit:30) hexPrintString
- (16r3fffffff clearBit:31) hexPrintString
- (16r3fffffff bitAt:29) hexPrintString
- (16r3fffffff bitAt:30) hexPrintString
- (16r3fffffff bitAt:31) hexPrintString
- (16r40000001 clearBit:1) hexPrintString
- (16rF0000001 clearBit:29) hexPrintString
- (16rF0000001 clearBit:30) hexPrintString
- (16rF0000001 clearBit:31) hexPrintString
- (16rF0000001 clearBit:32) hexPrintString
- (16r1F0000001 clearBit:33) hexPrintString
- "
-!
-
highBit
"return the bitIndex of the highest bit set. The returned bitIndex
starts at 1 for the least significant bit.
@@ -1735,48 +1632,6 @@
"
!
-invertBit:anInteger
- "return a new number where the specified bit is inverted.
- Bits are counted from 1 starting with the least significant.
- The method's name may be misleading: the receiver is not changed,
- but a new number is returned. Should be named #withBitInverted:"
-
-%{ /* NOCONTEXT */
-#ifndef __SCHTEAM__
- if (__isSmallInteger(anInteger)) {
- int index = __intVal(anInteger);
-
- if (index > 0) {
-# if __POINTER_SIZE__ == 8
- if (index <= 62)
-# else
- if (index <= 30)
-# endif
- {
- INT mask = __MASKSMALLINT((INT)1 << (index-1));
-
- RETURN ( ((OBJ) ((INT)self ^ (INT)mask)) );
- }
- }
- }
-#endif /* not __SCHTEAM__ */
-%}.
- ^ super invertBit:anInteger
-
- "
- (16r401 invertBit:2 ) hexPrintString
- (16r401 invertBit:1 ) hexPrintString
- (16r30000000 invertBit:1) hexPrintString
- (16r40000000 invertBit:0) hexPrintString
- (16r0 invertBit:29) hexPrintString
- (16r0 invertBit:30) hexPrintString
- (16r0 invertBit:31) hexPrintString
- (16r0 invertBit:32) hexPrintString
- (16r0 invertBit:33) hexPrintString
- (16r0 invertBit:100) hexPrintString
- "
-!
-
lowBit
"return the bitIndex of the lowest bit set. The returned bitIndex
starts at 1 for the least significant bit.
@@ -1982,6 +1837,153 @@
16 rightShift:2
4 rightShift:-2
"
+! !
+
+!SmallInteger methodsFor:'bit operators - indexed'!
+
+bitAt:anIntegerIndex
+ "return the value of the index's bit (index starts at 1) as 0 or 1.
+ Notice: the result of bitAt: on negative receivers is not
+ defined in the language standard (since the implementation
+ is free to choose any internal representation for integers)"
+
+%{ /* NOCONTEXT */
+#ifdef __SCHTEAM__
+ return context._RETURN( self.bitAt(anIntegerIndex));
+#else
+ if (__isSmallInteger(anIntegerIndex)) {
+ INT idx = __smallIntegerVal(anIntegerIndex);
+ if (idx > 0) {
+ if (idx > N_INT_BITS) {
+ RETURN(__mkSmallInteger(0));
+ }
+ RETURN((__smallIntegerVal(self) & ((INT)1 << (idx-1))) ? __mkSmallInteger(1) : __mkSmallInteger(0));
+ }
+ }
+#endif /* not __SCHTEAM__ */
+%}.
+
+ ^ SubscriptOutOfBoundsError
+ raiseRequestWith:anIntegerIndex
+ errorString:'index out of bounds'
+
+ "
+ 16r000000001 bitAt:0 -> error
+ 16r000000001 bitAt:1
+ 16r000000001 bitAt:2
+ 16r000008000 bitAt:16
+ 16r000800000 bitAt:24
+ 16r008000000 bitAt:28
+ 16r010000000 bitAt:29
+ 16r020000000 bitAt:30
+ 16r040000000 bitAt:31
+ 16r080000000 bitAt:32
+ 16r100000000 bitAt:33
+ "
+
+" Smalltalk implementation:
+ |mask|
+
+ anIntegerIndex <= 0 ifTrue:[
+ ^ SubscriptOutOfBoundsSignal
+ raiseRequestWith:anIntegerIndex
+ errorString:'index out of bounds'
+ ].
+ (anIntegerIndex > SmallInteger maxBits) ifTrue:[^ 0].
+ mask := 1 bitShift:(anIntegerIndex - 1).
+ ((self bitAnd:mask) == 0) ifTrue:[^ 0].
+ ^ 1
+"
+!
+
+clearBit:anInteger
+ "return a new integer where the specified bit is off.
+ Bits are counted from 1 starting with the least significant.
+ The method's name may be misleading: the receiver is not changed,
+ but a new number is returned. Should be named #withBitCleared:"
+
+%{ /* NOCONTEXT */
+#ifndef __SCHTEAM__
+ if (__isSmallInteger(anInteger)) {
+ int index = __intVal(anInteger);
+
+ if (index > 0) {
+# if __POINTER_SIZE__ == 8
+ if (index <= 62)
+# else
+ if (index <= 30)
+# endif
+ {
+ INT mask = __MASKSMALLINT( ((INT)1 << (index-1)));
+
+ RETURN ( ((OBJ) ((INT)self & ~(INT)mask)) );
+ }
+ RETURN (self); /* nothing to do ... */
+ }
+ }
+#endif /* not __SCHTEAM__ */
+%}.
+ ^ super clearBit:anInteger
+
+ "
+ (16r401 clearBit:1 ) hexPrintString
+ (16r401 clearBit:0 ) hexPrintString
+ (16r3fffffff clearBit:1) hexPrintString
+ (16r3fffffff clearBit:29) hexPrintString
+ (16r3fffffff clearBit:30) hexPrintString
+ (16r3fffffff clearBit:31) hexPrintString
+ (16r3fffffff bitAt:29) hexPrintString
+ (16r3fffffff bitAt:30) hexPrintString
+ (16r3fffffff bitAt:31) hexPrintString
+ (16r40000001 clearBit:1) hexPrintString
+ (16rF0000001 clearBit:29) hexPrintString
+ (16rF0000001 clearBit:30) hexPrintString
+ (16rF0000001 clearBit:31) hexPrintString
+ (16rF0000001 clearBit:32) hexPrintString
+ (16r1F0000001 clearBit:33) hexPrintString
+ "
+!
+
+invertBit:anInteger
+ "return a new number where the specified bit is inverted.
+ Bits are counted from 1 starting with the least significant.
+ The method's name may be misleading: the receiver is not changed,
+ but a new number is returned. Should be named #withBitInverted:"
+
+%{ /* NOCONTEXT */
+#ifndef __SCHTEAM__
+ if (__isSmallInteger(anInteger)) {
+ int index = __intVal(anInteger);
+
+ if (index > 0) {
+# if __POINTER_SIZE__ == 8
+ if (index <= 62)
+# else
+ if (index <= 30)
+# endif
+ {
+ INT mask = __MASKSMALLINT((INT)1 << (index-1));
+
+ RETURN ( ((OBJ) ((INT)self ^ (INT)mask)) );
+ }
+ }
+ }
+#endif /* not __SCHTEAM__ */
+%}.
+ ^ super invertBit:anInteger
+
+ "
+ (16r401 invertBit:2 ) hexPrintString
+ (16r401 invertBit:1 ) hexPrintString
+ (16r30000000 invertBit:1) hexPrintString
+ (16r40000000 invertBit:0) hexPrintString
+ (16r0 invertBit:29) hexPrintString
+ (16r0 invertBit:30) hexPrintString
+ (16r0 invertBit:31) hexPrintString
+ (16r0 invertBit:32) hexPrintString
+ (16r0 invertBit:33) hexPrintString
+ (16r0 invertBit:100) hexPrintString
+ "
!
setBit:anInteger