macro define and return value

hi all,

i came across below codes which i don't understand.
appreciate if you can provide some explainations.

what is the meaning of defining "0x1"?
what is the exact return value of "return statusFlags & FOUND_FROM_c++"?
what is meaning of "|="?

1
2
3
4
#define FOUND_FROM_c++              0x1
long    statusFlags
int foundFromc++()     { return statusFlags & FOUND_FROM_c++; }
void setFoundFromc++() { statusFlags |= FOUND_FROM_c++; }


thank you.
Well 0x1 is a hexadecimal value

and |= is bitwise or assigment

line 3 returns the bitwise and of status flag and 0x1

now since statusFlags is never initiated it is undefined.

does that help?
1. 0x1 is hexadecimal for the decimal number 1, which is kind of stupid...
2. "return statusFlags & FOUND_FROM_c++" would return statusFlags & 0x1, which equals statusFlags & 1, which equals statusFlags, no matter what.
3. "|=" is a C/C++ operator, called "bit-wise OR assignment" It performs operations on the individual bits, returning if at least one of the two bits being compared is on. Actually, x|=y can be expanded to x=x|y, "|" being an operator called bit-wise OR. The logic table of bit-wise OR (what bit-wise OR assignment is based off of) is as follows:

0|0==0
1|0==1
0|1==1
1|1==1
Try to see the output of the preprocessor. g++ -E

statusFlags & 1, which equals statusFlags, no matter what.
No, that will check the LSB
Check the types. long statusFlags; I think it is used as an array of bits.
hi guys,

thanks for your reply.

i wonder why do we want to use bit-wise OR return value. wouldn't it be easier if we just use return 1;
i checked the programs and i was not able to find where initiation of statusFlags happen.
if statusFlags never initiated, will it caused any problem in this case? like segmentation fault.

when statusFlags is not initiated, what is the return value of statusFlags for foundFromc++()?
what i mean is, is there any default value like 0 or space?
if the default value is space, it will return something like this " " & " " |= 0x1

when we return a value with ampersand, '&' sign, how does the program interpret it?
let say, 0 & 1

i am sorry if my questions sound stupid, but i really want to clear my doubts so that i dont have to ask for second times.

thank you.
AFAIK the compiler sets a global uninitialized variable to all zeros unless it has a constructor.

A "long" is generally going to be 32-bits or 64-bits on a 64 bit system. The two functions are either getting the value of the LSB (least significant bit) or setting the value of the LSB to 1. I don't know what you are talking about with "space". It is only dealing with integers, not characters. A global int will be set to 0. Local uninitialized variables get garbage.

& and | are "bitwise_and" and "bitwise_or" operators. These perform the corresponding logical operation for each corresponding bit of the operands and return the resulting bits. It s not returning a bitwise_or, it is using statusFlags |= 1 (equivalent to statusFlags = statusFlags | 1) to set the LSB of statusFlags to 1, while leaving the other 31 or 63 bits untouched.

Where did you get this code from anyway? AFAIK, you can't use "++" embedded in identifiers. That will not compile with g++. Also, no semi-colon after declaration of statusFlags.
thanks guys for your info. appreciate that.
Topic archived. No new replies allowed.