877 ^ self retry:#bitClear: coercing:anInteger |
877 ^ self retry:#bitClear: coercing:anInteger |
878 |
878 |
879 " |
879 " |
880 (2r001010100 bitClear:2r00001111) radixPrintStringRadix:2 |
880 (2r001010100 bitClear:2r00001111) radixPrintStringRadix:2 |
881 (2r111111111 bitClear:2r00001000) radixPrintStringRadix:2 |
881 (2r111111111 bitClear:2r00001000) radixPrintStringRadix:2 |
|
882 " |
|
883 ! |
|
884 |
|
885 bitCount |
|
886 "return the number of 1-bits in the receiver" |
|
887 |
|
888 %{ /* NOCONTEXT */ |
|
889 int _cnt = 0; |
|
890 int _self = __intVal(self); |
|
891 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
892 int _counts[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 }; |
|
893 |
|
894 _cnt = _counts[_self & 0xF]; _self = _self>>4; // ______xx |
|
895 _cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
896 if (_self != 0) { |
|
897 _cnt += _counts[_self & 0xF]; _self = _self>>4; // xxxxxx__ |
|
898 _cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
899 _cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
900 _cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
901 _cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
902 _cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
903 # if __POINTER_SIZE__ == 8 |
|
904 if (_self != 0) { |
|
905 _cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
906 _cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
907 _cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
908 _cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
909 _cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
910 _cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
911 _cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
912 _cnt += _counts[_self & 0xF]; _self = _self>>4; |
|
913 } |
|
914 # endif |
|
915 } |
|
916 |
|
917 RETURN ( __MKSMALLINT(_cnt)); |
|
918 %} |
|
919 |
|
920 " |
|
921 1 to:10000 do:[:n | |
|
922 self assert:(n bitCount = ((n printStringRadix:2) occurrencesOf:$1)) |
|
923 ] |
882 " |
924 " |
883 ! |
925 ! |
884 |
926 |
885 bitInvert |
927 bitInvert |
886 "return the value of the receiver with all bits inverted" |
928 "return the value of the receiver with all bits inverted" |