What's new

question on C


New member
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.


Mankind Member
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 ;)


Plugin coder / Betatester
*moved your thread to the programming forum, since this has nothing do to with dolphin itself...


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...