#### Archived

This topic is now archived and is closed to further replies.

# Divide by 2, right shift

This topic is 6088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How to divide a number by 2? Speaking assembly.

##### Share on other sites
shift to the high order

##### Share on other sites
I think it''s just:

shr eax, 1

##### Share on other sites
quote:
Original post by Beer Hunter
I think it''s just:

shr eax, 1

As long as you are talking about unsigned numbers,
otherwise you should use sar.

##### Share on other sites
actually, I was more looking for algorithm using only a few instructions like ADD, NOT, AND

##### Share on other sites
I don''t think you can divide just by using bitwise logic. But bit-shifting is pretty darn fast, so I don''t see why that wouldn''t be good enough. In fact, using multiple logical operations would undoubtedly be SLOWER!

##### Share on other sites
I''m tested for algorithm on limited machine instructions. Like for left shift or multiply by two, you would simply ADD the number by itself. There is away to do right shift, which I don''t know.

##### Share on other sites
There might be some weird way to do it, but why would you want to?

##### Share on other sites
There are some tricks (although mainly adds and subs) you can do by mixing 1''s complement operations with 2''s complement ops - (NOT vs NEG etc) and there are a few things you can do with masking against a few constants, but as mentioned above it''ll be slower than just using a bitwise shift (or using the FPU divide with good pipelining to hide the overhead).

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

• 35
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631358
• Total Posts
2999531
×