Omnimaga

Calculator Community => TI Calculators => ASM => Topic started by: TiAddict on June 07, 2011, 03:16:05 pm

Title: Question about bits
Post by: TiAddict on June 07, 2011, 03:16:05 pm
can someone explain about shifting bits, rotating, and etc.. I would appreciate if it also explain why you use it, when to use it.
Title: Re: Question about bits
Post by: Quigibo on June 07, 2011, 04:51:53 pm
Bit shifting is when you "shift" the number's binary representation left or right.  So the number %00110101 shifted left is %0110101X.  Why did I put an X there?  Each shift/rotate instruction does the same thing and the only difference between them is how it affects the new bit to be shifted in.  A rotate shifts in the bit that just got shifted out, the logical shifts will shift in a zero always (except sll which is undocumented and shifts in a 1 due to a hardware bug), the carry shifts will shift in the carry flag, and the arithmetic shift will keep the bit unchanged.

A logical shift left is equivalent to a multiply by 2, and a logical shift right is equivalent to a division by 2, and an arithmetic shift right is equivalent to a signed division by 2.  So these pop up a lot during math routines.  Another use is when you're reading something or writing something bit-by-bit, you'll generally use carry shifts to shift in each bit until all the bits have been shifted in and you have a result.  Yet another example is drawing things to the screen when your pixels don't line up with one of the whole bytes of the buffer, you have to shift them to align.  There are tons and tons more uses as well, but these are probably the most common.
Title: Re: Question about bits
Post by: AngelFish on June 07, 2011, 04:53:36 pm
Quote
A logical shift right is equivalent to a multiply by 2, and a logical shift right is equivalent to a division by...

Shouldn't the first one be a Logical Shift Left?

EDIT: Ninja fixed.
Title: Re: Question about bits
Post by: TiAddict on June 07, 2011, 07:27:32 pm
so you can use bit shifting to do math? like multiplying accumulator by two and diving it by two?
Title: Re: Question about bits
Post by: AngelFish on June 07, 2011, 07:33:28 pm
Yep.
Title: Re: Question about bits
Post by: thepenguin77 on June 08, 2011, 09:21:25 am
And actually, if you are clever, you can multiply by any number using shifts, although, it has to be the same number every time.

Since there is no SLA HL. We can either do SLA L \ RL H, or we can do ADD HL,HL which is the equivalent.

Here is the basic principle
100
4*25
4*(1+24)
4*(1+(8*3))
4*(1+(8*(1+2)))

as you can see, in that final line, it's mostly just shifts
with a few adds

Code: [Select]
multiplyHLBy100:
ld e, l
ld d, h ;de = 1

;multiply by 2
add hl, hl ;hl = 2

;add 1
add hl, de ;hl = 3

;multiply by 8
add hl, hl ;hl = 6
add hl, hl ;hl = 12
add hl, hl ;hl = 24

;add 1
add hl, de ;hl = 25

;multiply by 4
add hl, hl ;hl = 50
add hl, hl ;hl = 100
ret

Title: Re: Question about bits
Post by: Keoni29 on June 22, 2011, 12:25:15 pm
This image will explain everything. The point is that you think that it's just too complicated, but it's really simple. Be open minded like me and you'll understand.
Spoiler For Spoiler:
(http://)