Declaration Problem.

B

Bob Kensworth

Hello.

What do these two statements mean?

1)

#define stuff(x) (x >> 3)

2)

#define stuff1(x) (x & 7)


Thanks in advance,
Bob
 
T

Thomas Matthews

Bob said:
Hello.

What do these two statements mean?

1)

#define stuff(x) (x >> 3)

2)

#define stuff1(x) (x & 7)


Thanks in advance,
Bob

Look in your C reference manual under "bit manipulation"
or "bitwise operators". Both lines are macros. See the
section under "Preprocessor Directives".

The first line defines a macro that returns the parameter
right shifted 3 places (i.e. divided by 8).

The second one defines a macro which returns a value that
masks off the 3 least significant bits of the parameter.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
S

Steve Zimmerman

Bob said:
Hello.

What do these two statements mean?

1)

#define stuff(x) (x >> 3)

2)

#define stuff1(x) (x & 7)


Thanks in advance,
Bob

I'm not sure about 1. Number 2 means, "Perform a `logical and' operation
using x and 7 as operands."

This `logical and' operation is done on numbers in the base 2 number system
(binary number system). Seven is 0000 0111 in this binary (base 2) number
system. Now, let's say I take 1100 0011 and perform this `&' operation
on these two numbers. ---------


It's really simple. All you do is put a 1 underneath the dotted line when,
and only when, the two numbers above it are also 1. So,

0000 0111
& 1100 0011
---------
0000 0011

It's actually simpler than the addition and subtraction we learned
as children!

If you would like help on how to convert numbers to the binary (base 2) number
system, you may email me privately.

--Steve
 
S

Steve Zimmerman

Steve said:
I'm not sure about 1. Number 2 means, "Perform a `logical and' operation
using x and 7 as operands." ^^^^^^^^^^^



I'm sorry. It's not called a `logical and' operation; it's called a
`bitwise and' operation.

--Steve------------------
|

|
|
This `logical and' [I mean, `bitwise and'] operation is done on numbers in the base 2 number system
(binary number system). Seven is 0000 0111 in this binary (base 2) number
system. Now, let's say I take 1100 0011 and perform this `&'
operation
on these two numbers. ---------


It's really simple. All you do is put a 1 underneath the dotted line when,
and only when, the two numbers above it are also 1. So,

0000 0111
& 1100 0011
---------
0000 0011

It's actually simpler than the addition and subtraction we learned
as children!

If you would like help on how to convert numbers to the binary (base 2)
number
system, you may email me privately.

--Steve
 
P

pete

Bob said:
Hello.

What do these two statements mean?
1)

#define stuff(x) (x >> 3)

2)

#define stuff1(x) (x & 7)

Provided that x evaluates to a non negative integer:

1) divide by 8 (stuff(x) == x / 8)
2) modulus divide by 8 (stuff1(x) == x % 8)

If x evaluates to anything else other than a non negative integer,
then you're on your own.
 
P

pete

pete said:
Provided that x evaluates to a non negative integer:

1) divide by 8 (stuff(x) == x / 8)
2) modulus divide by 8 (stuff1(x) == x % 8)

If x evaluates to anything else other than a non negative integer,
then you're on your own.

If x evaluates to a negative integer,
then the values of the macros are implementation defined,
according to how negative integers are represented.

If x is does not evaluate to an integer,
then you have undefined behavior.
 
J

Jeff

Bob Kensworth said:
Hello.

What do these two statements mean?

1)

#define stuff(x) (x >> 3)

2)

#define stuff1(x) (x & 7)

"#define " defines a macro, when you use this in your code (in this source
file), it will be replaced before your compiler actually compile the code.

for example :

#define LENGTH 100

int main()
{
char mystring[LENGTH];
...

The above code will be finally translated to

int main()
{
char mystring[100];
....


Also, #define stuff(x) (x >> 3) and #define stuff1(x) (x & 7) are macro.
They takes a parameter (x) and insert it into the actual statement.

int main()
{
int foo = 10;
foo = stuff(foo);
....

It is equals to

int main()
{
int foo = 10;
foo = foo >> 3;


Read a C book or search Google if you don't know the meaning of ">>" and
"&" operator.
 
A

Aishwarya

pete said:
If x evaluates to a negative integer,
then the values of the macros are implementation defined,
according to how negative integers are represented.

If x is does not evaluate to an integer,
then you have undefined behavior.


I am sorry I am a little confused ... why would the behaviour be
undefined if x is a non-negative integer ?? macro 1 will shift the
bits to the right by 3 digits and macro 2 will perform a "and"
operation on the number with 7. Thus, even if x is a negative integer,
the macro 2 will mask the last 3 bits and hence return the remainder
of a division of the number by 8. macro 1, i aggree, will return a
value which is not x/8 , but then the behaviour is not undefined , is
it ? we can predict the output, cant we ? :)

I aggree non-integers may produce undefined behaviours :)

Cheers
Aish
 
T

Tom Zych

Bob said:
What do these two statements mean?
#define stuff(x) (x >> 3)
#define stuff1(x) (x & 7)

I'll skip explaining what they do since others have done that. One
important point about macros is that you should parenthesize each
parameter as well as the final result. Otherwise precedence and
associativity can bite you:

#define sqr(x) x*x
sqr(a+b) expands to a+b*a+b
1/sqr(2) expands to 1/2*2

#define sqr(x) ((x)*(x)) solves both problems.
 
R

Richard Bos

Tom Zych said:
Richard said:
(e-mail address removed) (Bob Kensworth) wrote:
What do these two statements mean?
[snip]

<checks calendar> Yup.

Meaning, it's time for student questions again? :)

Quite. More particularly, it's time for "I shouldn't have gone to
college at all, let alone taken programming as a subject, so please do
my homework, which is basic and still too hard for me to do myself"
questions.

Richard
 
I

Irrwahn Grausewitz

Tom Zych said:
I'll skip explaining what they do since others have done that. One
important point about macros is that you should parenthesize each
parameter as well as the final result. Otherwise precedence and
associativity can bite you:

#define sqr(x) x*x
sqr(a+b) expands to a+b*a+b
1/sqr(2) expands to 1/2*2

#define sqr(x) ((x)*(x)) solves both problems.

And, just to add my 5 microritchies to this thread, still be
aware of other possible side effects that may even break above
solution. For example:

#define sqr(x) ((x)*(x))

sqr(x++) expands to ((x++)*(x++))

Irrwahn
 
T

Tom Zych

Irrwahn said:
sqr(x++) expands to ((x++)*(x++))

Well, yes. That's a more general class of problems and not
restricted to macros, really. Macros just make it easier to
overlook.
 
P

pete

Aishwarya said:
Thus, even if x is a negative integer,
the macro 2 will mask the last 3 bits and hence return the remainder
of a division of the number by 8.

In sign and magnitude representation, (-1) & 7 equals 1.
In twos complement (-1) & 7 equals 7.
 
R

Richard Bos

Tom Zych said:
Well, yes. That's a more general class of problems and not
restricted to macros, really.

Actually, yes, it _is_ special to macros. If you call

sqr_function(x++)

what you get is the square of the current value of x, and x itself is
incremented by one. If, OTOH, you call

SQR_MACRO(x++)

what you get is

((x++)*(x++))

and that modifies x twice between sequence points, and therefore invokes
undefined behaviour.
That, BTW, is one of the reasons why it's customary to name macros in
ALL CAPS, because that the programmer using the macro realises that it
_is_ a macro, and will be more careful.

Richard
 
T

Tom Zych

Actually, yes, it _is_ special to macros. If you call

sqr_function(x++)

what you get is the square of the current value of x, and x itself is
incremented by one. If, OTOH, you call

SQR_MACRO(x++)

what you get is

((x++)*(x++))

and that modifies x twice between sequence points, and therefore invokes
undefined behaviour.
That, BTW, is one of the reasons why it's customary to name macros in
ALL CAPS, because that the programmer using the macro realises that it
_is_ a macro, and will be more careful.

Mmm, yes. You're right. I was thinking that x++ * x++ is bad whether
it comes from a macro or not, but one is a lot more obvious than the
other.
 
I

Irrwahn Grausewitz

Tom Zych said:
Mmm, yes. You're right. I was thinking that x++ * x++ is bad whether
it comes from a macro or not, but one is a lot more obvious than the
other.

That was just my understanding of your reply, so I didn't respond. :)

Irrwahn
 

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

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,206
Latest member
Zenden

Latest Threads

Top