How to resolve the algorithm Bitwise operations step by step in the Smalltalk programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Bitwise operations step by step in the Smalltalk programming language
Table of Contents
Problem Statement
Write a routine to perform a bitwise AND, OR, and XOR on two integers, a bitwise NOT on the first integer, a left shift, right shift, right arithmetic shift, left rotate, and right rotate. All shifts and rotates should be done on the first integer with a shift/rotate amount of the second integer. If any operation is not available in your language, note it.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Bitwise operations step by step in the Smalltalk programming language
Source code in the smalltalk programming language
| testBitFunc |
testBitFunc := [ :a :b |
('%1 and %2 is %3' % { a. b. (a bitAnd: b) }) displayNl.
('%1 or %2 is %3' % { a. b. (a bitOr: b) }) displayNl.
('%1 xor %2 is %3' % { a. b. (a bitXor: b) }) displayNl.
('not %1 is %2' % { a. (a bitInvert) }) displayNl.
('%1 left shift %2 is %3' % { a. b. (a bitShift: b) }) displayNl.
('%1 right shift %2 is %3' % { a. b. (a bitShift: (b negated)) }) displayNl.
].
testBitFunc value: 16r7F value: 4 .
(a bitClear: b) "mask out bits"
(a bitAt: index) "retrieve a bit (bit-index, one-based)"
(a setBit: index) "set a bit (bit-index)"
(a clearBit: index) "clear a bit (bit-index)"
(a invertBit: index) "invert a bit (bit index)"
lowBit "find the index of the lowest one-bit; zero if none"
highBit "find the index of the highest one-bit; zero if none"
bitCount "count the one-bits"
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the R programming language
You may also check:How to resolve the algorithm Globally replace text in several files step by step in the Fortran programming language
You may also check:How to resolve the algorithm Elementary cellular automaton step by step in the Ruby programming language
You may also check:How to resolve the algorithm Loops/While step by step in the C# programming language
You may also check:How to resolve the algorithm Generic swap step by step in the Groovy programming language