Which Is Better Option to Use for Dividing an Integer Number by 2

Which is better option to use for dividing an integer number by 2?

Use the operation that best describes what you are trying to do.

  • If you are treating the number as a sequence of bits, use bitshift.
  • If you are treating it as a numerical value, use division.

Note that they are not exactly equivalent. They can give different results for negative integers. For example:

-5 / 2  = -2
-5 >> 1 = -3

(ideone)

Should I bit-shift to divide by 2 in Java?

Unless you're working in a shop and a codebase where bit-shifting is common then, IMHO, you're risking obfuscation. Yes, the expressions may be logically equivalent but:

  • A n00b might get confused by the alternate syntax
  • An old guy who hasn't had to do any bit-shifting since college, like myself, might get confused
  • If you bit shift and feel the need to comment on what you just did then you're definitely off. Simple division is self-documenting and would be clear to anyone who's familiar with elementary math
  • You're not going to outsmart a compiler for optimization on something that simple so don't bother trying
  • As good coding practice it's better to make your code simple/vanilla rather than clever(er)

All this is relative and, again, really depends on your shop's standards. If your colleagues love to bit-shift, then by all means go forth and bit-shift.

Why is such complex code emitted for dividing a signed integer by a power of two?

The reason is that unsigned division by 2^n can be implemented very simply, whereas signed division is somewhat more complex.

unsigned int u;
int v;

u / 4096 is equivalent to u >> 12 for all possible values of u.

v / 4096 is NOT equivalent to v >> 12 - it breaks down when v < 0, as the rounding direction is different for shifting versus division when negative numbers are involved.

Divide Signed Integer By 2 compiles to complex assembly output, not just a shift

It is to get the correct "rounding towards zero" result for negative numbers. Division by shifting rounds towards negative infinity, so negative numbers will have a different result compared to the expected result of the C division operator.

An example is -1: shifting right by 1 gives -1 still, but the C operator / 2 gives 0.

So the extra code is a correction for this effect. If you don't need that, use unsigned or an explicit shift (but the second option is less portable).

Dividing an integer with a double

setprecision(n); tells the maximum number n of digits to use, not the minimum. Keep in mind that trailing zeroes are automatically discarded, this is what happens when you divide 3 by 3.0, the result is 1.000... but the zeroes get discarded. If you want to get n digits at all times you have to use std::fixed like this:

cout << fixed;
cout << setprecision(2);

and it will give you 2 digits, so for instance 3 / 3.0 will yield 1.00.

Integer division: How do you produce a double?

double num = 5;

That avoids a cast. But you'll find that the cast conversions are well-defined. You don't have to guess, just check the JLS. int to double is a widening conversion. From §5.1.2:

Widening primitive conversions do not
lose information about the overall
magnitude of a numeric value.

[...]

Conversion of an int or a long value
to float, or of a long value to
double, may result in loss of
precision-that is, the result may lose
some of the least significant bits of
the value. In this case, the resulting
floating-point value will be a
correctly rounded version of the
integer value, using IEEE 754
round-to-nearest mode (§4.2.4).

5 can be expressed exactly as a double.

Dividing two numbers out of a integer

You could cast the values:

response = (float)(a[0][1]) / (float)(a[0][0]) * 100

This accomplishes the same thing as your explicitly introduced variables (and if you need an intergral response, you can wrap this in another layer of casting).



Related Topics



Leave a reply



Submit