How do I find the lowest bit position of a mask using only macros?
I want to do everything in compile time. That mean, there cannot be
control statements such as if, while, for, etc. in the macro.
Note that the macro takes only one (1) argument, the mask.
Examples:
// Mask Lowest bit position
0111 0000 4
0001 0000 4
0001 1000 3
0011 1111 0
Thanks. 11 10508 ed*****@yahoo.c om (ex laguna) writes: How do I find the lowest bit position of a mask using only macros?
Presumably you mean the lowest bit set to 1.
I want to do everything in compile time. That mean, there cannot be control statements such as if, while, for, etc. in the macro.
#define LOWEST_1_BIT(MA SK) \
(((MASK) & 0001) ? 0 : /* bit 0 is set? */
((MASK) & 0002) ? 1 : /* bit 1 is set? */
((MASK) & 0004) ? 2 : /* bit 2 is set? */
((MASK) & 0010) ? 3 : /* bit 3 is set? */
((MASK) & 0020) ? 4 : /* bit 4 is set? */
/* ... */
-1) /* no bits are set */
--
char a[]="\n .CJacehknorstu" ;int putchar(int);in t main(void){unsi gned long b[]
={0x67dffdff,0x 9aa9aa6a,0xa77f fda9,0x7da6aa6a ,0xa67f6aaa,0xa a9aa9f6,0x1f6}, *p=
b,x,i=24;for(;p +=!*p;*p/=4)switch(x=*p& 3)case 0:{return 0;for(p--;i--;i--)case
2:{i++;if(1)bre ak;else default:continu e;if(0)case 1:putchar(a[i&15]);break;}}}
On 25 Jul 2003 10:23:20 -0700, ed*****@yahoo.c om (ex laguna) wrote: How do I find the lowest bit position of a mask using only macros?
I want to do everything in compile time. That mean, there cannot be control statements such as if, while, for, etc. in the macro.
Note that the macro takes only one (1) argument, the mask.
Examples:
// Mask Lowest bit position 0111 0000 4 0001 0000 4 0001 1000 3 0011 1111 0
You could do that with a lot of ?: operators in sequence, but
you'd need N operators where N is the number of bits.
Alternative you could use lg N macros, like (off the cuff)
#define LOWESTBITPOS2( x ) (x & 0x1? 0 : 1)
#define LOWESTBITPOS4( x ) (x & 0x3? LOWESTBITPOS2( x ) : 2+LOWESTBITPOS2 ( x >> 2 ))
#define LOWESTBITPOS8( x ) (x & 0xF? LOWESTBITPOS4( x ) : 4+LOWESTBITPOS4 ( x >> 4 ))
#define LOWESTBITPOS16( x ) (x & 0xFF? LOWESTBITPOS8( x ) : 8+LOWESTBITPOS8 ( x >> 8 ))
#define LOWESTBITPOS32( x ) (x & 0xFFFF? LOWESTBITPOS16( x ) : 16+LOWESTBITPOS 16( x >> 16 ))
#define LOWESTBITPOS( x ) LOWESTBITPOS32( x )
If this works, then I wrote it. If not, then some unscroupolous
evil charlatan impersionated me. Anyway, note the assumption of 32
bits maximum.
Also note the assumption of at least one 1-bit.
If that's not the case you need to decide what the result should
be for a bitpattern of all zeros.
On 25 Jul 2003 14:22:41 -0700, ed*****@yahoo.c om (ex laguna) wrote: al***@start. no (Alf P. Steinbach) wrote in message news:<3f******* ********@News.C IS.DFN.DE>... Also note the assumption of at least one 1-bit.
If that's not the case you need to decide what the result should be for a bitpattern of all zeros.
Yes, the assumption is that the mask is a non-zero constant number known at compile time.
So my question is, would all these "?" statements use any CPU at run time?
Not for a compile-time constant... ;-)
Whether they would for a run-time call is, however, a Quality Of
Implementation issue, i.e. that depends on your compiler.
ex laguna wrote: al***@start.no (Alf P. Steinbach) wrote in message news:<3f******* ********@News.C IS.DFN.DE>... Also note the assumption of at least one 1-bit.
If that's not the case you need to decide what the result should be for a bitpattern of all zeros.
Yes, the assumption is that the mask is a non-zero constant number known at compile time.
So my question is, would all these "?" statements use any CPU at run time?
Probably not. The expression is a "constant expression,"
meaning that it can be evaluated at compile time and could
be used anywhere a constant can be used. However, there is
no absolute guarantee in the Standard that an expression
that *can* be evaluated at compile time *will* be evaluated
at compile time. An implementation that didn't do so would
certainly be regarded as perverse, but ...
-- Er*********@sun .com ed*****@yahoo.c om (ex laguna) wrote in message news:<53******* *************** **@posting.goog le.com>... How do I find the lowest bit position of a mask using only macros?
I want to do everything in compile time. That mean, there cannot be control statements such as if, while, for, etc. in the macro.
Note that the macro takes only one (1) argument, the mask.
Examples:
// Mask Lowest bit position 0111 0000 4 0001 0000 4 0001 1000 3 0011 1111 0
Thanks.
If your mask is an unsigned integer then (mask & -mask) will produce
the lowest masked bit. That's not what you asked for, but it may be
suitable for your underlying problem.
--
Peter
On Fri, 25 Jul 2003 20:52:59 -0400, Peter Nilsson wrote: If your mask is an unsigned integer then (mask & -mask) will produce the lowest masked bit. That's not what you asked for, but it may be suitable for your underlying problem.
....and if you multiply or divide by the result of that it gives you the
same result as shifting by the index of that bit. For example if mask
0x08 is bit 4:
val / (0x08 & -0x08)
is the same as
val >> 4
So if the OP is going to use the value to shift they could multiply or
divide instead.
This leads to a nice macro for decoding bitfields:
#define FLD(i, m) (m != 0 ? ((i) & (m)) / ((m) & -(m)) : 0)
Mike
Michael B Allen <mb*****@ioplex .com> wrote in message news:<pa******* *************** ************@io plex.com>... On Fri, 25 Jul 2003 20:52:59 -0400, Peter Nilsson wrote:
If your mask is an unsigned integer then (mask & -mask) will produce the lowest masked bit. That's not what you asked for, but it may be suitable for your underlying problem.
...and if you multiply or divide by the result of that it gives you the same result as shifting by the index of that bit. For example if mask 0x08 is bit 4:
val / (0x08 & -0x08)
is the same as
val >> 4
So if the OP is going to use the value to shift they could multiply or divide instead.
This leads to a nice macro for decoding bitfields:
#define FLD(i, m) (m != 0 ? ((i) & (m)) / ((m) & -(m)) : 0)
Mike
Thank you very much Mike. This is exactly what I am looking for. I am
writing a communications message protocol parser, so I only have to
define the bit masks of the messages.
Best Regards,
Ex Laguna ed*****@yahoo.c om (ex laguna) wrote in message news:<53******* *************** @posting.google .com>... Michael B Allen <mb*****@ioplex .com> wrote in message news:<pa******* *************** ************@io plex.com>... On Fri, 25 Jul 2003 20:52:59 -0400, Peter Nilsson wrote:
If your mask is an unsigned integer
I should have added "of int rank or higher..."
then (mask & -mask) will produce the lowest masked bit. That's not what you asked for, but it may be suitable for your underlying problem. ...and if you multiply or divide by the result of that it gives you the same result as shifting by the index of that bit. For example if mask 0x08 is bit 4:
val / (0x08 & -0x08)
That's potential division by zero. Use 0x08u. is the same as
val >> 4
ITYM val >> 3 So if the OP is going to use the value to shift they could multiply or divide instead.
This leads to a nice macro for decoding bitfields:
#define FLD(i, m) (m != 0 ? ((i) & (m)) / ((m) & -(m)) : 0)
Might as well have ((m) != 0 ? ...
Thank you very much Mike. This is exactly what I am looking for. I am writing a communications message protocol parser, so I only have to define the bit masks of the messages.
As I thought, the old communication problem: I need to solve A, I
*think* I need to do B, so I'll ask how to solve B... ;)
--
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Charles Law |
last post by:
Sorry for posting this here, but although I have posted to the Infragistics
group, it is not so frequently used, and I fear a long wait for a response.
There are undoubtedly Infragistics users here, so hopefully someone can
offer a pointer or two.
I want to position a balloon tip (of my own making) above a panel in an
UltraWinStatusBar. Can anyone tell me how I can find the position of a panel
in the status bar?
I have tried working...
|
by: Sunny123 |
last post by:
hello
i am trying to find the maximum value of a function and where it occur.
i.e there is an array
x |y
=============
|
|
|
by: sagar |
last post by:
is there any way to find the position of mouse click on a form
actually the problem is i m having more than one controls on a form i
want to find which control is selected using mouse down
any help is fine for me
thanks
|
by: Sunil Varma |
last post by:
Hi,
I've to write a function similar to this.
int process(const vector<int>& vct,int key)
{
// Here I've to find the position of key in the vector and do some
processing.
}
|
by: vunet.us |
last post by:
I have a DIV element. How can I find mouse position (top and left)
inside of this DIV?
<div onMouseMove="getPositions();"
style="width:200px;height:100px"></div>
function getPositions(ev){
????????????????
}
| |
by: Patrick Fisher |
last post by:
Hi
I have tables from 12 suppliers each of whom can supply the same part,
I need to be able to create a table or query containing a list of
suppliers who can supply at the lowest price for each item. I am
struggling with this, can anyone help.
Thanks
Patrick
|
by: slizorn |
last post by:
hi guys,
i need to make a tree traversal algorithm that would help me search the tree..
creating a method to search a tree to find the position of node and to return its pointer value
basically i need to read in a text file... shown below
H
H,E,L
E,B,F
B,A,C
A,null,null
|
by: NKTA |
last post by:
Hello again.
I have a doubt in how to obtain the position of an image in a table cell.
What i want to do is:
- Find the image position, and add a new image on top of that image.
The problem, is that the image is inserted in the table so doing with the offset (Left / Top) gives its position regarding its offsetparent and posts the nem image in the wrong place. How do i discorver the image position regarding the document itself ?
The...
|
by: vibhakhushi |
last post by:
I have an array
@myarray = ("father","mother","sister","brother");
How to find the position of "sister" in @myarray
I didn't get any built in functions to do this. I'm a newbie in Perl. Please suggest some good tutorial.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |