3 ms·
I love these early days of programming as I do it as hobby. What's pretty amazing is how much you can do without any multiplication or division at all. With a
by wvenable 3y ago
I love these early days of programming as I do it as hobby. What's pretty amazing is how much you can do without any multiplication or division at all. With add, substract, and shifts you can do a surprising amount on a computer.
- gary_0 3y agoWith shifts, you can multiply and divide by any number you want, as long as it's a power of two. ;)
- wvenable 3y ago"Doctor, my arm really hurts when I bend it this way, do you have any advice?" "Don't bend your arm that way" You can honestly get around not needing arbitrary multiplication and division by just avoiding numbers that aren't powers of two. No oddly sized data structures, for example. It works well unless, of course, you're building a calculator.
- whartung 3y agoYou can do it by any number if you toss in addition. If you’re multiplying by a constant, you can factor it out. For example multiply by 40 (coincidentally the number of characters per line on common microcomputers), you could shift to multiply by 32, shift to multiply by 8, and add them together. Took some additional storage but was faster, particularly for 16 bit, than a general routine.
- dhosek 3y agoI remember “discovering” shift and add as a high school student for doing multiply by 40 (a useful operation for screen mapping) as an improvement over the more naïve x×y = x+x+…(y times)…+x. But seeing the table of squares thing, I see the genius of being able to take (x+y)²-(x-y)²=(x²+2xy+y²)-(x²-2xy+y²)= 4xy and what a brilliant piece of math that is.
- wkjagt 3y agoI do quite a bit of 6502 assembly (hobby only). And the only types of multiplications I’ve needed were either multiplying by a power of 2 (often if I’m using a data table where an entry is 2 bytes long and I need to calculate the index into the table), or multiplying by a known constant, which is solved by shift and add or a lookup table if I only ever need to multiply a small number of known values.