View Full Version : question on C
googy
June 7th, 2006, 15:44
Need help with C++(in this case it is Visual, but for this question I think it doesn't really matter):
Suppose we have a structure called A. Consider it's element type uint32 called B. What does this line mean:
(A.B >> 21)& 0x1f
What does operator >> do in this case? & I would use for a pointer, but 0x1f is just a number, what & 0x1f mean? How does this construction work? I never had experience working with such a construction.
NoeOM
June 7th, 2006, 16:56
the >> operator does bitwise right shift (and the << operator does left). For example, let x be a 8 bit variable with the following value
x = 00101100
then, after executing this piece of C (and C++) code
x << 1;
the value of x is:
x = 01011000
this operation (<<) can also be seen as a fast method of calculate some products since
b = a << 1; is equivalent to b = a * 2;
b = a << 2; is equivalent to b = a * 4;
and so on, IF WE CONSIDER THAT THERE ARE NO OVERFLOW PROBLEMS (1s going out of the variable by the "left side").
The & operator is the bitwise AND operator. After the following C statement
c = a & b;
the i-th bit of c is active (1) if the i-th bit of a is active AND the i-th bit of b is active. For example, let a and b be two 8 bit variables with the following values
a = 01010101
b = 00001111
-------------
a & b = 00000101
And 0x1f is an hexadecimal number. In C, when you prefix a number with 0x, you mean the number is in hexadecimal format. 0x1f is just 31 in decimal format.
Sorry for my bad english. I hope I have help you ... a bit ;)
googy
June 7th, 2006, 17:00
Thanks a lot!
NoeOM
June 7th, 2006, 17:02
Not at all. Ask whenever you want!
Falcon4ever
June 8th, 2006, 10:35
*moved your thread to the programming forum, since this has nothing do to with dolphin itself...
Toasty
June 8th, 2006, 12:06
A Wikipedia write-up on bitwise operators. (http://en.wikipedia.org/wiki/Bitwise) There's a few other handy ones there too (XOR, NOT, OR). All of which include C/C++ examples of such operators.
Doomulation
June 8th, 2006, 16:09
For the record, & is the address of operator and bitwise and. & can be used to get the address of a variable, which can be used to init pointers or simply simply print the address (most commonly used for pointers). It isn't just for pointers... good to know. Well, not really, but anyway...
googy
June 8th, 2006, 18:11
Thanks
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.