Bitwise & operator question 
Author Message
 Bitwise & operator question

Can any one tell me how I can divide an integer by a power of 2 using
the bitwise & operator and the restrictions of doing this?

Thanks,
Doug



Thu, 23 Aug 2001 03:00:00 GMT  
 Bitwise & operator question


Quote:
> Can any one tell me how I can divide an integer by a power of 2 using
> the bitwise & operator and the restrictions of doing this?

Using &? Well, I spose it's /possible/ but are you SURE you don't mean >> ?

A >> B right-shifts A by B bits. Each shift to the right divides by 2,
losing any remainder. Its effect on negatives can be surprising, though, so
take care.

Whilst I would love to spend the time working out how to do this with &,
right now I don't /have/ the time. Maybe tonight.

--
Richard H

#include "sig.h"



Sat, 25 Aug 2001 03:00:00 GMT  
 Bitwise & operator question

Quote:
>Well, that's an interesting and correct solution (provided x is unsigned)
>but it does not answer the poster's original question.
>Instead, he might consider the following backhanded clue:

<snip>

Quote:
>If we couple this behavior with the way that modulus and division are
>related we might think about bit patterns and find an answer.? Or not.

That is an absolutely correct observation, but a little bit of
boolean algebra will show you that it is not possible to implement
bit-shift (eg division by a power of two) without using at least one
other operator.

For reference's sake, any* operation that deals with integers can
be coded using only the operations NOT and AND, and therefore by
DeMorgan's Theorem, NOT and OR. This is a provable fact.

* When I say "any", I say "any" because I can't be bothered to think
? of a proper defintion which really means something between
? "pretty much any" and "anything you'd want to do".

Cheers,
Wes

--


PageMail, Inc.??????????????????? |
Kingston, ON Canada?????????????? | Voice: (888) 247 6246



Tue, 28 Aug 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Bitwise & operator tricky question

2. Bitwise & operator with int

3. Questions - Numeric formats and bitwise operators

4. Question about bitwise operators

5. && operator question

6. bitwise operators

7. bitwise-inclusive-OR operator

8. Bitwise complement operator with [Flags] attribute on C# enum

9. bitwise operator on a struct

10. NAND bitwise operator

11. bitwise operators don't like ushort

12. More on Bitwise Operators (~)

 

 
Powered by phpBB® Forum Software