Hello.
What do these two statements mean?
1)
#define stuff(x) (x >> 3)
2)
#define stuff1(x) (x & 7)
Thanks in advance,
Bob 17 2160
Bob Kensworth wrote: 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
Steve Zimmerman wrote: Bob Kensworth wrote:
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." ^^^^^^^^^^^
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
Bob Kensworth wrote: 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.
--
pete
pete wrote: Bob Kensworth wrote: 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.
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.
--
pete
"Bob Kensworth" <Bo*******@yahoo.com> wrote in message
news:FB*********************@weber.videotron.net.. . 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.
--
Jeff Bo*******@yahoo.com (Bob Kensworth) wrote: What do these two statements mean?
1)
#define stuff(x) (x >> 3)
2)
#define stuff1(x) (x & 7)
<checks calendar> Yup.
Richard
pete <pf*****@mindspring.com> wrote in message news:<3F***********@mindspring.com>... pete wrote: Bob Kensworth wrote: 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.
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
Richard Bos wrote: Bo*******@yahoo.com (Bob Kensworth) wrote:
What do these two statements mean?
[snip]
<checks calendar> Yup.
Meaning, it's time for student questions again? :)
--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Bob Kensworth wrote: 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.
--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Tom Zych <tz******@pobox.com> wrote: Richard Bos wrote:
Bo*******@yahoo.com (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
Tom Zych <tz******@pobox.com> wrote: Bob Kensworth wrote:
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.
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
--
What does this red button do?
Irrwahn Grausewitz wrote: 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.
--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Aishwarya wrote: pete <pf*****@mindspring.com> wrote in message news:<3F***********@mindspring.com>... pete wrote: Bob Kensworth wrote: > > 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.
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.
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.
--
pete
Tom Zych <tz******@pobox.com> wrote: Irrwahn Grausewitz wrote:
sqr(x++) expands to ((x++)*(x++))
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
Richard Bos wrote: sqr(x++) expands to ((x++)*(x++))
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.
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.
--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Tom Zych <tz******@pobox.com> wrote: Richard Bos wrote:
> > sqr(x++) expands to ((x++)*(x++)) > 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.
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
--
Close your eyes and press escape three times. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Andrew Ward |
last post by:
Hi All,
Could someone please tell me why this code does not compile.
struct A {};
struct B {
B(A &) {}
};
struct C {
|
by: Bhushit Joshipura |
last post by:
This post contains one question and one proposal.
A. May I know why order of evaluation of arguments is not specified in
C/C++?
I asked a question in comp.lang.c++ for the following...
|
by: Vu Pham |
last post by:
I think this problem relates to either c or c++ ( but I am not sure which
one ) so I post to both of these news group. I am sorry if I did something
wrong here.
Is there any difference between...
|
by: François Côté |
last post by:
I'm generating a TLB from one of my assemblies that contains an interface
declaration. The class that implements this interface is in another assembly.
I import this tlb in my C++ code to create...
|
by: Legendary Pansy |
last post by:
Hello, I'm trying to accomplish the impossible by trying to do something equivalent of this example found here
http://www.c-sharpcorner.com/Code/2003/Dec/DialogTutorial.as
Starting with "Listing...
|
by: Henning Hasemann |
last post by:
Hi all,
On larger projects I used the following 'technique' (well in fact its a
really simple thing):
1. Write one source-code file for each 'large' class and one
corresponding header file....
|
by: Jess |
last post by:
Hello,
I was told that if I declare a static class constant like this:
class A{
static const int x = 10;
};
then the above statement is a declaration rather than a definition.
As I've...
|
by: Alan |
last post by:
Does a template class declaration, like
template <class T>
have to come immediately prior to the declaration of the function,
e.g.,
T do_something (T something)
{ . . . }
that uses it?
|
by: NeoPa |
last post by:
Problem Description :
In VBA there is an option to enforce declaration of variables in your code. With this set, any reference to a variable that has not been previously declared (Dim; Private;...
|
by: WaterWalk |
last post by:
I find friend declaration just very tricky. I tried the following
examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are
surprising.
Example1:
namespace ns1
{
class Test
{
friend...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |