473,464 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to define macro to get bitwidth of a constant?

This is what I am doing now:

#define SIZE 512
#defube SIZE_WIDTH 9

But are there any way to write a preprocessor command to calculate the SIZE_WIDTH from SIZE ? This is what I want to do, but cannot pass compiler:

#define SIZE_WIDTH LOG2(SIZE)

Thanks for any feedback!
Nov 19 '10 #1
4 5293
mac11
256 Contributor
I'm not sure if this is a question about preprocessor syntax or implementation detail (or maybe there's an unseen design consideration). The sample code you put up won't compile, so are you confused as to syntax?

There is a log function in math.h and you could use that (and maybe put it in a function rather than a macro).

There are various ways to figure out the position of the highest bit for integer values as well, which may suit your needs (see http://graphics.stanford.edu/~seander/bithacks.html)

You asked for a very specific implementation detail with no surrounding design context. How are you using SIZE_WIDTH (what are you using it to do)? I mean, whats the overall task you're trying to accomplish? Maybe there's a better approach to be taken.
Nov 19 '10 #2
donbock
2,426 Recognized Expert Top Contributor
There are ways to find the bit-width during execution-time. I'm not aware of any way to do it during compile-time.
Nov 19 '10 #3
newb16
687 Contributor
Expand|Select|Wrap|Line Numbers
  1. void printlog(int left, int right)
  2. {
  3.   if (left==right)
  4.   {
  5.     printf("%d", left);
  6.     return;
  7.   }
  8.   int m = (left+right)/2;
  9.   printf(" ( (i)>=%du)?", 1<<(m+1));
  10.   printlog(m==right?m:m+1,right);
  11.   printf(" : ");
  12.   printlog(left, m); 
  13. }
  14. printlog(0, 5);
  15.  
This will generate something like
( (i)>=8u)? ( (i)>=32u)?5 : ( (i)>=16u)?4 : 3 : .....
that you can define as some #define LOG2(i), and use it at compile time, e.g. for array dimension.
Nov 20 '10 #4
newb16,

What an interesting way to solve a problem!

Yes, this is what I am looking for. I wish the C preprocessor should have included this code to save us from having to generate the code by running another code.
Nov 22 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Dead RAM | last post by:
Hey people, i'll try to keep this short ;) Here is what I want to type (or at least close too)... #define VER_BUILD 1 #define STR_VER_BUILD "VER_BUILD" But what happends is...
1
by: Michelle | last post by:
I'm working on code that has several (approx 50) very similar objects. They all inherit from a base class that can contain several of the methods I need, but the methods that specifically need the...
13
by: Rick Anderson | last post by:
Group, I want to define a #include directive from another macro, if possible... For example, the following doesn't work but is basically what I need to do: #define...
1
by: SpreadTooThin | last post by:
How do I define a constant that I can use in my script... For example lets say I have a file called constants.py and in there I have PI = 3.14 in my test script I do: from constants import *...
9
by: blangela | last post by:
Can somepoint me to a good discussion on the pros and cons of using #define versus a constant variable in your C+ application? Thanks, Bob
1
by: George2 | last post by:
Hello everyone, I have a number of strings in an array, "FILE1", "FILE2", "FILE3", ... , "FILEN" I want to add the common prefix to all the string, like (for example, the common prefix...
6
by: JohnZ | last post by:
Hi, I'm trying to define a macro that calls a log function: #define LOG(level, device, pcString, args ...) if (level < SERIAL_LOG_LEVEL) _log(device, pcString, ## args) void _log (unsigned...
9
by: Mara Guida | last post by:
Is there a suffix for size_t? I want to define MAX_LINE_LENGTH as a size_t. The best I could come up with was #define MAX_LINE_LENGTH ((size_t)80) but I wonder if there's a more...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.