Greatest number divisible by Y

  • Thread starter Frederick Gotham
  • Start date
H

Hallvard B Furuseth

Peter said:
I didn't say the top most bit of an integer _type_. I'm talking about
top most value bit with value 1 for an _arbitrary_ binary number.

You can do it up to a chosen limit though. I posted some compile-
time floor(log2()) variants for up to 512 bits in article
<[email protected]> (thread Value Bits Vs Object Bits),
with similar algorithms to the log10()s needed in this thread.
 
D

Dave Thompson

Must be a single constant/literal, decimal (which in C automatically
means no leading* zero), integer (no decimal point or fractional
digits or exponent). (And no sign not even redundant plus.)

(* For the usual arithmetic meaning of 'leading', i.e. redundant, not
the stringwise meaning of contiguous to beginning. The number zero has
a single digit zero which is at the beginning but not redundant.)
static int powersOfTen[] =
{ 1
, 10
, 100
, 1000
, 10000
, 100000
, 1000000
, 10000000
, 100000000
,1000000000
};
Requires 32-bit (or at least 30-bit) int. long would be safer.
/* Returns the power of 10 with the same number of digits as X */
#define maxPowerOfTen(X) (powersOfTen[sizeof(#X)-2])

Doesn't work, eg. maxPowerOfTen(UINT_MAX) is always 7 because
#UINT_MAX is "UINT_MAX".

Even if you [indirect] might be [hex, etc.]

And even with these fixes(?) it doesn't produce something the C++
compiler considers a constant = compile-time expression, which is what
the PP wanted. (In C++ unlike C, a const int or enum (scalar)
initialized with a constant expression can be used, but still not an
array. Or for that matter function call, even if available and pure.)

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top