Author Topic: Question about bits  (Read 3557 times)

0 Members and 1 Guest are viewing this topic.

Offline TiAddict

  • LV3 Member (Next: 100)
  • ***
  • Posts: 68
  • Rating: +0/-0
    • View Profile
Question about bits
« 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.

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: Question about bits
« Reply #1 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.
« Last Edit: June 07, 2011, 04:52:49 pm by Quigibo »
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Question about bits
« Reply #2 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.
« Last Edit: June 07, 2011, 04:55:08 pm by Qwerty.55 »
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline TiAddict

  • LV3 Member (Next: 100)
  • ***
  • Posts: 68
  • Rating: +0/-0
    • View Profile
Re: Question about bits
« Reply #3 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?

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Question about bits
« Reply #4 on: June 07, 2011, 07:33:28 pm »
Yep.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: Question about bits
« Reply #5 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

« Last Edit: June 08, 2011, 09:22:29 am by thepenguin77 »
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline Keoni29

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2466
  • Rating: +291/-16
    • View Profile
    • My electronics projects at 8times8
Re: Question about bits
« Reply #6 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:
« Last Edit: June 24, 2011, 05:43:38 am by Keoni29 »
If you like my work: why not give me an internet?