this post was submitted on 28 Mar 2024
368 points (93.6% liked)

Programmer Humor

32572 readers
275 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] PowerCrazy@lemmy.ml 1 points 8 months ago (1 children)

Hey how is "bit shifting" different then division? (The answer may surprise you).

[–] LodeMike@lemmy.today 5 points 8 months ago (1 children)

Bit shifting works if you wanna divide by 2 only.

[–] PowerCrazy@lemmy.ml 2 points 8 months ago (1 children)

interesting, so does the computer have a special "base 10" ALU that somehow implements division without bit shifting?

[–] nybble41@programming.dev 4 points 8 months ago (1 children)

In general integer division is implemented using a form of long division, in binary. There is no base-10 arithmetic involved. It's a relatively expensive operation which usually requires multiple clock cycles to complete, whereas dividing by a power of two ("bit shifting") is trivial and can be done in hardware simply by routing the signals appropriately, without any logic gates.

[–] PowerCrazy@lemmy.ml 1 points 8 months ago

In general integer division is implemented using a form of long division, in binary.

The point of my comment is that division in binary IS bitshifting. There is no other way to do it if you want the real answer. You can estimate, you can round, but the computational method of division is done via bitshifting of binarary expansions of numbers in an ALU.