Trivia question for you Bill

# Thread: Trivia question for you Bill

1. Senior Member
Join Date
Dec 1969
Posts
635

## Trivia question for you Bill

I&#039;m adressing this question you since I think you&#039;re probable the only one nerd enough here to know the answer but anyone is welcome to explain.<BR><BR>For some time, I&#039;ve been wandering how the computer does a division.<BR><BR>I always took for granted that the computer would do the same thing I do when I make a calculation on paper but for the division I can&#039;t believe it does it as stupidly as I do.<BR><BR>When I divide for instance 10 by 2, I guess there is 5 times 2 in 10.<BR><BR>in 546 / 3, I&#039;d start by guessing how many times 3 in 500, then in 46...etc.<BR><BR>But that&#039;s way too clumsy for a computer to do that.<BR><BR>So I&#039;ve been thinking.... using &#062;&#062; and &#060;&#060; (left & right logical shift) could serve as a quick calculation.<BR><BR>10 &#062;&#062; 2 = 5 ??<BR><BR>[binary equivalent]<BR><BR>I&#039;ll stick to 8-bits for simplicity sake<BR>1010 &#062;&#062; 0010 = 0010 ... ?? that&#039;s not right.<BR><BR>I always sucked at binary....HELP!!<BR><BR>I wanna understand how a division is made.<BR><BR>Of course, feel free to tell me I should search on Google instead of bugging you, that would be fair but I like when you explain it, you make it sound simple :)<BR><BR>Eniac<BR><BR><BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
11,334

## I know some of it

Computers can&#039;t multiply, subtract, or divide... they can only add (albeit very quickly). Division happens with recipriocals.<BR><BR>Bit shifting is okay as long as you&#039;re not on the first or the last bit, since then you&#039;ll overpass it. Also, it depends on if you&#039;re using a signed bit or not (the sign is typically the first bit).<BR><BR>Here&#039;s from MSDN:<BR><BR>These binary operators have left-to-right associativity.<BR><BR>Both operands of the shift operators must be of integral types. Integral promotions are performed according to the rules described in Integral Promotions. The type of the result is the same as the type of the left operand. The value of a right-shift expression e1 &#062;&#062; e2 is e1 / 2e2, and the value of a left-shift expression e1 &#060;&#060; e2 is e1 * 2e2.<BR><BR>The results are undefined if the right operand of a shift expression is negative or if the right operand is greater than or equal to the number of bits in the (promoted) left operand.<BR><BR>The left shift operator causes the bit pattern in the first operand to be shifted left the number of bits specified by the second operand. Bits vacated by the shift operation are zero-filled. This is a logical shift, as opposed to a shift-and-rotate operation.<BR><BR>The right shift operator causes the bit pattern in the first operand to be shifted right the number of bits specified by the second operand. Bits vacated by the shift operation are zero-filled for unsigned quantities. For signed quantities, the sign bit is propagated into the vacated bit positions. The shift is a logical shift if the left operand is an unsigned quantity; otherwise, it is an arithmetic shift.<BR><BR>Microsoft Specific<BR><BR>The result of a right shift of a signed negative quantity is implementation dependent. Although Microsoft C++ propagates the most-significant bit to fill vacated bit positions, there is no guarantee that other implementations will do likewise.<BR><BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
11,334

## RE: Trivia question for you Bill

http://www.avr-asm-tutorial.net/avr_en/calc/DIVISION.html<BR><BR><BR>Help any?

4. Senior Member
Join Date
Dec 1969
Posts
96,118

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## One typo in that...

Says:<BR>subtract dvr from dvd. dvd is now 0000011<BR>so add adb to cnt. cnt is now 0001000<BR>right shift adb and dvr<BR><BR>First line there should say:<BR>subtract dvr from dvd. dvd is now 0000101<BR><BR>(register summary below that has it right).<BR><BR>********<BR><BR>It helps if you view all that in Courier font.<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
635

## Geez...

How do you guys know all that....<BR><BR>Thanks a lot DG & Bill.<BR><BR>Although, I&#039;m disapointed to realize that a computer not more clever than I am when dividing, LOL....just keep substracting until you fall below zero.<BR><BR>Well..no...actually, I&#039;ll take it on the bright side. I&#039;m AS clever as a computer... (is that good ??)<BR><BR>LOL. Anyway, it all makes sense, thanks a lot to both of you for your time.

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## Should have said...

...that&#039;s just one way of doing it. Another way is to shift the dvr to the right. And there are variations.<BR><BR>

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## Should have said...

...that&#039;s just one way of doing it. Another way is to shift the dvd to the left. And there are variations.<BR><BR>

9. Senior Member
Join Date
Dec 1969
Posts
96,118

## This is the easy stuff...

Now try coding *FLOATING POINT* division!<BR><BR>7.3311 / 38291.229718<BR><BR>and get the answer right to 53 bits of accuracy.<BR><BR>I used to do this for a living. Code multiplication and division in software. And code all the floating point ops in software (because those primitive computers didn&#039;t have *ANY* floating point hardware operations).<BR><BR>And then I got to code the SIN(), COS(), ATN(), EXP(), LOG(), and the exponentition operator ( ^ or ** depending on language ).<BR><BR>I was pretty good at it. And now it&#039;s an "art" that is dead, since it&#039;s all done in the CPUs.<BR><BR>

10. Member
Join Date
Dec 1969
Posts
41

## The best programmers..

..are those who understand mathematics. Some of the best programmers I&#039;ve encountered are theoretical mathematicians, making their own programs and algorithms to do their research.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•