473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Amount of bits needed (as a compile time constant)


Anyone know a good compile-time constant to tell you how many bits are
needed to store a given value?

For instance:

CALC_BITS_NEEDED(0) == 1
CALC_BITS_NEEDED(1) == 1
CALC_BITS_NEEDED(2) == 2
CALC_BITS_NEEDED(3) == 2
CALC_BITS_NEEDED(4) == 3
CALC_BITS_NEEDED(7) == 3
CALC_BITS_NEEDED(8) == 4

I need it as a compile-time constant coz I'll be using as an array
size. I first though of trying IMAX_BITS but it only works on numbers
where all bits are 1.

Martin

Sep 23 '07 #1
8 1902
Martin Wells wrote:
Anyone know a good compile-time constant to tell you how many bits are
needed to store a given value?

For instance:

CALC_BITS_NEEDED(0) == 1
CALC_BITS_NEEDED(1) == 1
CALC_BITS_NEEDED(2) == 2
CALC_BITS_NEEDED(3) == 2
CALC_BITS_NEEDED(4) == 3
CALC_BITS_NEEDED(7) == 3
CALC_BITS_NEEDED(8) == 4

I need it as a compile-time constant coz I'll be using as an array
size. I first though of trying IMAX_BITS but it only works on numbers
where all bits are 1.

Martin
Hi Martin

#include <math.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc,char *argv[])
{
double d;

if (argc < 2) {
printf("Usage: %s <number>\n",argv[0]);
return EXIT_FAILURE;
}
d = strtod(argv[1],NULL);
printf("%g\n",ceil(log2(d)));
}

Compile that, and then insert the answer in your program.
I do not see any way to do it at compile time other than
that.
Sep 23 '07 #2
Martin wrote:
) Anyone know a good compile-time constant to tell you how many bits are
) needed to store a given value?

I'm not sure if the Standard requires it, but on my compiler, a?b:c reduces
to a constant if the arguments are constants.

So you could use something like:

#define CALC_BITS_02(x) ((x) ? 1+CALC_BITS_03((x)>>1) : 0)
#define CALC_BITS_01(x) ((x) ? 1+CALC_BITS_02((x)>>1) : 0)
#define CALC_BITS_NEEDED ((x) ? 1+CALC_BITS_01((x)>>1) : 0)

and just copy/paste as many as you need, increasing the number each time.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Sep 23 '07 #3
Willem <wi****@stack.nlwrites:
Martin wrote:
) Anyone know a good compile-time constant to tell you how many bits are
) needed to store a given value?

I'm not sure if the Standard requires it,
It is.
but on my compiler, a?b:c reduces
to a constant if the arguments are constants.

So you could use something like:

#define CALC_BITS_02(x) ((x) ? 1+CALC_BITS_03((x)>>1) : 0)
#define CALC_BITS_01(x) ((x) ? 1+CALC_BITS_02((x)>>1) : 0)
#define CALC_BITS_NEEDED ((x) ? 1+CALC_BITS_01((x)>>1) : 0)
Typo: you intended (x) after CALC_BITS_NEEDED.

One could also do something like:

#define CALC_BITS_NEEDED1(x) ((x) & 0x1u)
#define CALC_BITS_NEEDED2(x) ((x) & 0x2u ? 1+CALC_BITS_NEEDED1((x) >1) \
: CALC_BITS_NEEDED1((x) & 0x1u))
#define CALC_BITS_NEEDED4(x) ((x) & 0xcu ? 2+CALC_BITS_NEEDED2((x) >2) \
: CALC_BITS_NEEDED2((x) & 0x3u))
#define CALC_BITS_NEEDED8(x) ((x) & 0xf0u ? 4+CALC_BITS_NEEDED4((x) >4) \
: CALC_BITS_NEEDED4((x) & 0xfu))
#define CALC_BITS_NEEDED16(x) ((x) & 0xff00u ? 8+CALC_BITS_NEEDED8((x) >8) \
: CALC_BITS_NEEDED8((x) & 0xffu))
#define CALC_BITS_NEEDED32(x) ((x) & 0xffff0000lu ? \
16+CALC_BITS_NEEDED16((x) >16) : \
CALC_BITS_NEEDED16((x) & 0xffffu))

or shorten that by:

#define CALC_BITS_NEED4(x) ((x) 1 ? 2 : (x) & 1)

--
Ben.
Sep 23 '07 #4

I'm not sure how I'd achieve this, but is there any way I could get
the high bit on it's own? If so, I'd do it in the following steps:

1: Take the original number 1010
2: Take just the high bit 1000
3: Subtract 1 from it 111
4: Shift it once to the left and then add 1 1111
5: Use IMAX_BITS to give 4

Something like:

#define BITS_NEEDED(x) ( IMAX_BITS( (JUST_HIGH_BIT((x)) - 1 << 1) +
1 ) )

So now I just need a formula for JUST_HIGH_BIT ;)

Any ideas?

Martin
Sep 24 '07 #5
Martin wrote:
)
) I'm not sure how I'd achieve this, but is there any way I could get
) the high bit on it's own? If so, I'd do it in the following steps:
)
) 1: Take the original number 1010
) 2: Take just the high bit 1000
) 3: Subtract 1 from it 111
) 4: Shift it once to the left and then add 1 1111
) 5: Use IMAX_BITS to give 4
)
) Something like:
)
) #define BITS_NEEDED(x) ( IMAX_BITS( (JUST_HIGH_BIT((x)) - 1 << 1) +
) 1 ) )
)
) So now I just need a formula for JUST_HIGH_BIT ;)

I think that formula would be roughly the same as the formulae that
were posted already to calculate the result you wanted.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Sep 24 '07 #6
Martin Wells <wa****@eircom.netwrites:
I'm not sure how I'd achieve this, but is there any way I could get
the high bit on it's own? If so, I'd do it in the following steps:

1: Take the original number 1010
2: Take just the high bit 1000
3: Subtract 1 from it 111
4: Shift it once to the left and then add 1 1111
5: Use IMAX_BITS to give 4

Something like:

#define BITS_NEEDED(x) ( IMAX_BITS( (JUST_HIGH_BIT((x)) - 1 << 1) +
1 ) )

So now I just need a formula for JUST_HIGH_BIT ;)

Any ideas?
I don't know what IMAX_BITS is (does it count 1s?) but steps 1 to 4
can be done by (for max 32-bit unsigned):

x |= x >1;
x |= x >2;
x |= x >4;
x |= x >8;
x |= x >16;

You probably want that as a constant expression, which looks mighty
fiddly to do. I am tempted to ask what the high-level objective is.
In other words, maybe all this can be avoided?

--
Ben.
Sep 24 '07 #7
Martin Wells wrote:
Anyone know a good compile-time constant to tell you how many bits are
needed to store a given value?

For instance:

CALC_BITS_NEEDED(0) == 1
CALC_BITS_NEEDED(1) == 1
CALC_BITS_NEEDED(2) == 2
CALC_BITS_NEEDED(3) == 2
CALC_BITS_NEEDED(4) == 3
CALC_BITS_NEEDED(7) == 3
CALC_BITS_NEEDED(8) == 4

I need it as a compile-time constant coz I'll be using as an array
size. I first though of trying IMAX_BITS but it only works on numbers
where all bits are 1.
Given that the intended use is for an array size, it
seems safe to suppose that the numbers are non-negative
and well within the range of unsigned long long or maybe
even unsigned long. That makes the possibilities easy
to enumerate, as in:

#define CALC_BITS_NEEDED(x) (1 \
+ ((x) 1) \
+ ((x) 3) \
+ ((x) 7) + \
+ ... \
+ ((x) 0x...FFF) \
)

Use as many terms as you think the numbers might need:
thirty-two, perhaps, for arrays up to four giga-elements.
You might want to incorporate one of the "compile-time
assertion" hacks for a range check, perhaps by rewriting
the initial constant term along these lines:

#define CALC_BITS_NEEDED(x) ( \
sizeof(char[(x) >= 0 && (x) <= 0x...FFF]) \
+ ...

--
Eric Sosman
es*****@ieee-dot-org.invalid
Sep 24 '07 #8
Eric:
I need it as a compile-time constant coz I'll be using as an array
size. I first though of trying IMAX_BITS but it only works on numbers
where all bits are 1.

Given that the intended use is for an array size, it
seems safe to suppose that the numbers are non-negative
and well within the range of unsigned long long or maybe
even unsigned long. That makes the possibilities easy
to enumerate, as in:

#define CALC_BITS_NEEDED(x) (1 \
+ ((x) 1) \
+ ((x) 3) \
+ ((x) 7) + \
+ ... \
+ ((x) 0x...FFF) \
)

Use as many terms as you think the numbers might need:
thirty-two, perhaps, for arrays up to four giga-elements.
You might want to incorporate one of the "compile-time
assertion" hacks for a range check, perhaps by rewriting
the initial constant term along these lines:

#define CALC_BITS_NEEDED(x) ( \
sizeof(char[(x) >= 0 && (x) <= 0x...FFF]) \
+ ...

I think this is the best option so far. Thanks a lot Eric, you're a
great help.

If anyone else has more ideas though please throw them out there.

Martin

Sep 24 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Lionel B | last post by:
Running VC++ 6 under Win2K on i386. I would like to assign a (compile-time) constant that resolves to a quiet NaN (of type double) I can assign a quiet NaN to a *variable* (of type const...
12
by: Frederick Gotham | last post by:
Over on comp.lang.c, Hallvard B Furuseth devised a compile-time constant representing the amount of value representation bits in an unsigned integer type. In true C fashion, here it is as a macro:...
3
by: Frederick Gotham | last post by:
(I'm not sure if there's already something in the Standard Library for doing this... ?) Is the following macro okay for getting a compile-time constant that indicates which negative number...
1
by: Frederick Gotham | last post by:
I've mentioned his name a few times here lately, but over on comp.lang.c, Hallvard B Furuseth has come up with a Log-base-2 function that serves as a compile-time constant. I'm not going to...
22
by: Tomás Ó hÉilidhe | last post by:
I've been developing a C89 microcontroller application for a while now and I've been testing its compilation using gcc. I've gotten zero errors and zero warnings with gcc, but now that I've moved...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.