Arithmetic shift divison question

In summary, the conversation discusses a function that uses shifts to perform a calculation and how it compares to a simpler formula. There is confusion about how the function works and why it includes certain components. The conversation also brings up the possibility of the function not being accurate for certain inputs. The final question asks about the data types of the variables used in the calculation.
  • #1
bitrex
193
0
If I have the following function:

a = b * c/255

The following function is apparently equivalent using only shifts:

product = b * c;
a = (product + (product>>8) + 1)>>8;

I am having trouble following how this function works. Since an arithmetic right shift is division by a power of 2 right shift 8 would be division by 256...somehow (product + (product>>8) +1) is compensating for the difference between the two but I'm not sure why it's that and not just (product + 1). I'm too thickheaded to see it, any clarification would be appreciated!
 
Technology news on Phys.org
  • #2
I don't think your formula is correct. For example, if b was 0, then b*c/255 is zero but your replacement formula would not produce 0.
 
  • #3
The algorithm fails at 65535 (255*257) and larger numbers.

The algorithm is based on the fact that in hexadecimal fixed point

1/ff = .010101010101 ...

or expressed as a power series:

p/255 = (p/256) + (p/2562) + (p/2563) + (p/2564) + (p/2565) + ...
 
Last edited:
  • #4
are b and c integers? this might work if integers are stored as two's complement.
 

1. What is an arithmetic shift division?

An arithmetic shift division is a mathematical operation that involves shifting the binary representation of a number to the right. It is equivalent to dividing the number by a power of two.

2. How is an arithmetic shift division different from a regular division?

Unlike regular division, which involves dividing a number by another number, arithmetic shift division only involves shifting the bits of a number to the right. This operation is used in computer programming for efficient multiplication and division by powers of two.

3. Can an arithmetic shift division result in a decimal number?

No, an arithmetic shift division can only result in an integer. This is because it involves shifting the bits of a number, which does not change the actual value of the number, but rather its representation in binary form. Therefore, the result will always be a whole number.

4. What are some practical applications of arithmetic shift division?

Arithmetic shift division is commonly used in computer programming for efficient multiplication and division by powers of two. It is also used in digital signal processing, data compression, and encryption algorithms.

5. How is an arithmetic shift division performed?

To perform an arithmetic shift division, the binary representation of a number is shifted to the right by a specified number of bits. This is equivalent to dividing the number by a power of two. If the number is shifted by n bits, the result will be the original number divided by 2^n.

Similar threads

  • Programming and Computer Science
Replies
17
Views
2K
Replies
4
Views
676
  • Programming and Computer Science
Replies
12
Views
1K
Replies
6
Views
2K
  • Astronomy and Astrophysics
Replies
3
Views
856
  • Programming and Computer Science
Replies
3
Views
1K
  • Precalculus Mathematics Homework Help
Replies
3
Views
1K
  • Calculus and Beyond Homework Help
Replies
4
Views
1K
  • Calculus and Beyond Homework Help
Replies
3
Views
278
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top