Can anyone please explain this to me -- thanks

A

almurph

Hi,

I need some help with this below:


#define Func1(a) (1 << (a))
#define Func2(a,b) ((b[(a) >> 3]) & Func1((a) & 07))

where b is of type int
where s is an array of type - I'm not too sure, think its an arrays on
ints but I'm open on this could be chars

I understand the concept of macros in C but the above confuses me.
What is the author doing at the end of the day? Can this be
simplified? Would appreciate any comments/ideas/joined-up-thinking
post that you may be able to offer.

Thanking you,
Al.
 
B

Ben Bacarisse

I need some help with this below:

#define Func1(a) (1 << (a))
#define Func2(a,b) ((b[(a) >> 3]) & Func1((a) & 07))

One would guess b is intended to be a char array (I'd use unsigned
char) and then Func2(a, b) is zero or non-zero depending on the value
of the 'a'th bit in the bytes pointer to by b.

Tabulating (the last column is in binary):

a (a) >> 3 (a) & 7 1 << ((a) & 7)
0 0 0 1
1 0 1 10
2 0 2 100
.... ... ... ...
6 0 6 1000000
7 0 7 10000000
8 1 0 1
9 1 1 10
10 1 2 100
.... ... ... ...
15 1 7 10000000
16 2 0 1

Func2 takes a byte from b -- for a=0..7 it is the first byte, for
a=8..15 it is the second, and so on. The & in Func2 masks this byte
with a value that contains a single bit set, thus selecting one bit
from the chosen byte.

Given the names, I guess the code was intended to confuse rather than
help the reader.
 
J

James Dow Allen

#define Func1(a) (1 << (a))
#define Func2(a,b) ((b[(a) >> 3]) & Func1((a) & 07))

I've used this idiom *very* often and am slightly amazed many
responders pretended never to have seen it. (I also found the
suggestion to use a real function wrong-headed.)

Here's the actual formula I use in one of my on-line programs
http://james.fabpedigree.com/gsieve.htm
*Helpful* criticism appreciated. I've included the first
line (application-dependent MAXN definition) just so the fragment is
(almost) self-sufficient.


#define MAXN 140 /* largest Numn allowed */

typedef u_short Bmword;
#define LOGBPB 4 /* log of number of bits in Bmword */

/* Bit mask macros */
#define NUMBPB (1 << LOGBPB)
#define BMSIZ ((MAXN + NUMBPB) / NUMBPB)
typedef Bmword Bitmask[BMSIZ];
#define TESTBIT(sp, n) ((sp)[(n) >> LOGBPB] & 1 << ((n) & (NUMBPB - 1)))
#define CLRBIT(sp, n) ((sp)[(n) >> LOGBPB] &= ~(1 << ((n) & (NUMBPB - 1))))
#define SETBIT(sp, n) ((sp)[(n) >> LOGBPB] |= 1 << ((n) & (NUMBPB - 1)))

The macro names, TESTBIT, etc. seem self-explanatory. Bmword, etc.
less so, perhaps, but only MAXN, Bitmask, TESTBIT, CLRBIT and SETBIT
would ever be typed once the above code is present.

James Dow Allen
 
P

Phil Carmody

pete said:
Hi,

I need some help with this below:


#define Func1(a) (1 << (a))
#define Func2(a,b) ((b[(a) >> 3]) & Func1((a) & 07))

where b is of type int
where s is an array of type - I'm not too sure, think its an arrays on
ints but I'm open on this could be chars

I understand the concept of macros in C but the above confuses me.
What is the author doing at the end of the day? Can this be
simplified? Would appreciate any comments/ideas/joined-up-thinking
post that you may be able to offer.

#define Func2(a, b) (b[a / 8] & Func1(a % 8))

What on earth would make you prefer something broken over
something that's ugly byt works?

Try ``Func2(12+i,b);'' in your code, for example.

Phil
 
A

almurph

pete said:
Hi,
    I need some help with this below:
#define         Func1(a)               (1 << (a))
#define         Func2(a,b)     ((b[(a) >> 3]) & Func1((a) & 07))
where b is of type int
where s is an array of type - I'm not too sure, think its an arrays on
ints but I'm open on this could be chars
       I understand the concept of macros in C but the above confuses me.
What is the author doing at the end of the day? Can this be
simplified? Would appreciate any comments/ideas/joined-up-thinking
post that you may be able to offer.
#define Func2(a, b)    (b[a / 8] & Func1(a % 8))

What on earth would make you prefer something broken over
something that's ugly byt works?

Try ``Func2(12+i,b);'' in your code, for example.

Phil
--
I tried the Vista speech recognition by running the tutorial. I was
amazed, it was awesome, recognised every word I said. Then I said the
wrong word ... and it typed the right one. It was actually just
detecting a sound and printing the expected word! -- pbhj on /.- Hide quoted text -

- Show quoted text -

Thanks for all your comments, but can anybody explain what it is doing
in general?
Confused,
Al.
 
B

Ben Bacarisse

(e-mail address removed) wrote:
Hi,
    I need some help with this below:
#define         Func1(a)               (1 << (a))
#define         Func2(a,b)     ((b[(a) >> 3]) & Func1((a) & 07))
<snip sig>

It is best if you don't quote sig blocks.
Thanks for all your comments, but can anybody explain what it is doing
in general?

I thought I did. If James Dow Allen's recent post does not finish off
the explanation for you, it would be better to pick an answer that
does not quite explain it and ask a specific question about that
explanation.
 
B

Barry Schwarz

pete said:
(e-mail address removed) wrote:
Hi,
    I need some help with this below:
#define         Func1(a)               (1 << (a))
#define         Func2(a,b)     ((b[(a) >> 3]) & Func1((a) & 07))
where b is of type int
where s is an array of type - I'm not too sure, think its an arrays on
ints but I'm open on this could be chars
       I understand the concept of macros in C but the above confuses me.
What is the author doing at the end of the day? Can this be
simplified? Would appreciate any comments/ideas/joined-up-thinking
post that you may be able to offer.
#define Func2(a, b)    (b[a / 8] & Func1(a % 8))
What on earth would make you prefer something broken over
something that's ugly byt works?
Try ``Func2(12+i,b);'' in your code, for example.
Phil
--
I tried the Vista speech recognition by running the tutorial. I was
amazed, it was awesome, recognised every word I said. Then I said the
wrong word ... and it typed the right one. It was actually just
detecting a sound and printing the expected word! -- pbhj on /.- Hide quoted text -
- Show quoted text -

Thanks for all your comments, but can anybody explain what it is doing
in general?
Confused,
Al.- Hide quoted text -

- Show quoted text -

It isn't doing anything until it is used in code.

x = Func1(y); will set x equal to the value of 2 raised to the y-th
power. In some sense it is an efficient integer version of the
expression pow(2,y).

Func2 was explained in a previous reply.
 
B

Bart

pete said:
(e-mail address removed) wrote:
Hi,
    I need some help with this below:
#define         Func1(a)               (1 << (a))
#define         Func2(a,b)     ((b[(a) >> 3]) & Func1((a) & 07))
where b is of type int
where s is an array of type - I'm not too sure, think its an arrays on
ints but I'm open on this could be chars
       I understand the concept of macros in C but the above confuses me.
What is the author doing at the end of the day? Can this be
simplified? Would appreciate any comments/ideas/joined-up-thinking
post that you may be able to offer.
#define Func2(a, b)    (b[a / 8] & Func1(a % 8))
What on earth would make you prefer something broken over
something that's ugly byt works?
Try ``Func2(12+i,b);'' in your code, for example.
Phil
--
I tried the Vista speech recognition by running the tutorial. I was
amazed, it was awesome, recognised every word I said. Then I said the
wrong word ... and it typed the right one. It was actually just
detecting a sound and printing the expected word! -- pbhj on /.- Hide quoted text -
- Show quoted text -

Thanks for all your comments, but can anybody explain what it is doing
in general?
Confused,
Al.- Hide quoted text -

- Show quoted text -

It returns the a'th bit of char array b.

So with a=0..7, it returns a bit from byte 0; with a=8..15, from byte
1 and so on.

The value is returned as either 0, or one of 1,2,4,8,16,32,64,128; I
would have put !! in front of the whole thing to return a 0 or 1.

The code assumes an 8-bit char size.
 

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
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top