| View previous topic :: View next topic |
| Author |
Message |
BlackDragon777
Joined: 15 Sep 2005 Posts: 32
|
Posted: Sat Sep 24, 2005 1:40 am Post subject: Shift operator in Matrix Function Question |
|
|
Hmmm I read the post that shine sent me about rotating triangles and their properties. I have a question though.
Why does the author of the code choose to use a shift operation for the indicies of his array of numbers in his matrix?
Here is an example of his code
| Code: |
void matrix_identity(float* matrix)
{
matrix[(0<<2)+0] = 1.0f;
matrix[(0<<2)+1] = 0.0f;
matrix[(0<<2)+2] = 0.0f;
matrix[(0<<2)+3] = 0.0f;
matrix[(1<<2)+0] = 0.0f;
matrix[(1<<2)+1] = 1.0f;
matrix[(1<<2)+2] = 0.0f;
matrix[(1<<2)+3] = 0.0f;
matrix[(2<<2)+0] = 0.0f;
matrix[(2<<2)+1] = 0.0f;
matrix[(2<<2)+2] = 1.0f;
matrix[(2<<2)+3] = 0.0f;
matrix[(3<<2)+0] = 0.0f;
matrix[(3<<2)+1] = 0.0f;
matrix[(3<<2)+2] = 0.0f;
matrix[(3<<2)+3] = 1.0f;
}
|
I see that this is a 4x4 matrix of verticies. I also understand what he his doing. For example,
That would come out to being the index of 15, the value in his matrix. My question is isn't it easier to just index like so,
Is there any added bonus or benefit by indexing with shift operations?
Please enlighten me. |
|
| Back to top |
|
 |
ooPo Site Admin
Joined: 17 Jan 2004 Posts: 2032 Location: Canada
|
Posted: Sat Sep 24, 2005 2:23 am Post subject: |
|
|
My guess is ease of reading?
| Code: | | matrix[(2<<2)+0] = 0.0f; |
A quick glance lets you know you're modifying (2,0).
The compiler should also optimize it to a static number when it gets compiled so performance isn't a concern. |
|
| Back to top |
|
 |
BlackDragon777
Joined: 15 Sep 2005 Posts: 32
|
Posted: Sat Sep 24, 2005 4:03 am Post subject: |
|
|
| Oh! Ok I see it now! Yeah I guess that would help a lot with readability. Thanks ooPo |
|
| Back to top |
|
 |
|