Reputation:
I have read in my book this sentence:
Logical and Shift Instructions Operate on bits individually Unlike arithmetic, which operate on entire word
Use to isolate fields. Either by masking or by shifting back and forth.
I can not understand these two sentences at all.
Both shift and arithmetic instructions would change all the bits (in some situation), and in arithmetic, bits are added one by one to calculate the answer.
So what is the meaning of this part "Instructions Operate on bits individually Unlike arithmetic, which operate on entire word"?
My second question:
I don't know have any idea about this part can you explain it to me:
Use to isolate fields. Either by masking or by shifting back and forth.
My third question: what is a logical instruction. Did he mean AND, OR for example? Can you explain more?
Upvotes: 3
Views: 212
Reputation: 41764
Logical and Shift operations are commonly called bitwise operations. They operate on bits individually which means that each output bit depends on just bit(s) from a single fixed bit position in the input(s) and you can calculate that bit immediately without depending on any other bits or previous calculation results
For example in AND, OR, XOR... output[n]
(i.e. bit n in the output) is calculated from input1[n]
and input2[n]
. Similarly left shift by N produces output[i]
from only input[i - N]
, and complementing (NOT) converts from input[n]
to output[n]
. I'm taking the sample images from here so that it's easier to understand
OTOH each bit in the addition result depends on the carry in and you must wait for the previous add operations to complete like this Sn = An + Bn + Cn
It's possible to do faster additions by pre-calculating the carry in with some logic like in the carry-lookahead adder, but it's still slower and needs a lot more die area than bitwise operations that have no dependants. Similarly other arithmetic operations also can't be done by getting each output bit individually.
That's for your first and third questions. Regarding the second one
Use to isolate fields Either by masking or by shifting back and forth.
means they're used to get bit fields by masking with AND (i.e. let only bits bits in that field fall through, by filtering out bits in positions where the mask is zero), or by shifting out to zero the bits and shift back to their original position
Further reading:
Upvotes: 8
Reputation: 11537
Logical instructions are and, or, xor, but I do not understand either the first sentence.
Concerning the second, assume that you are interested by part of the word, say bits 6..4 and you wand to extract this information. There are two ways to do that.
using masking and shifting. You set up a mask with ones on the field that interests you and zeros elsewhere and apply an AND with this mask.
unsigned original ; // initial data is yyy...yyyxxxyyyy
// where the interesting part is coded by xxx
unsigned field ; // we want to have field=00..0xxx
unsigned mask = 0x70 ; // 0...01110000 ones on the interesting part
field = original & mask ; // field=0...00xxx0000
field >>= 4 ; // field=00...00xxx
using only shifts
unsigned original, field ; // initial data and interesting field
field = original << 25 ; // put interesting bits in the msb of field
// field=xxxyyyy00..00
field >>= 29 ; // get rid of the 4 LSB -> field=00...00xxx
Similar methods can be used if you want to modify these bits instead of extracting them.
Upvotes: 1