Shift operator in Matrix Function Question

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
BlackDragon777
Posts: 32
Joined: Thu Sep 15, 2005 8:26 am

Shift operator in Matrix Function Question

Post by BlackDragon777 »

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: Select all


void matrix_identity(float* matrix)
{
   matrix&#91;&#40;0<<2&#41;+0&#93; = 1.0f;
   matrix&#91;&#40;0<<2&#41;+1&#93; = 0.0f;
   matrix&#91;&#40;0<<2&#41;+2&#93; = 0.0f;
   matrix&#91;&#40;0<<2&#41;+3&#93; = 0.0f;

   matrix&#91;&#40;1<<2&#41;+0&#93; = 0.0f;
   matrix&#91;&#40;1<<2&#41;+1&#93; = 1.0f;
   matrix&#91;&#40;1<<2&#41;+2&#93; = 0.0f;
   matrix&#91;&#40;1<<2&#41;+3&#93; = 0.0f;

   matrix&#91;&#40;2<<2&#41;+0&#93; = 0.0f;
   matrix&#91;&#40;2<<2&#41;+1&#93; = 0.0f;
   matrix&#91;&#40;2<<2&#41;+2&#93; = 1.0f;
   matrix&#91;&#40;2<<2&#41;+3&#93; = 0.0f;

   matrix&#91;&#40;3<<2&#41;+0&#93; = 0.0f;
   matrix&#91;&#40;3<<2&#41;+1&#93; = 0.0f;
   matrix&#91;&#40;3<<2&#41;+2&#93; = 0.0f;
   matrix&#91;&#40;3<<2&#41;+3&#93; = 1.0f;
&#125;

I see that this is a 4x4 matrix of verticies. I also understand what he his doing. For example,

Code: Select all

matrix&#91;&#40;3<<2&#41;+3&#93;
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,

Code: Select all

matrix&#91;15&#93;
Is there any added bonus or benefit by indexing with shift operations?

Please enlighten me.
ooPo
Site Admin
Posts: 2023
Joined: Sat Jan 17, 2004 9:56 am
Location: Canada
Contact:

Post by ooPo »

My guess is ease of reading?

Code: Select all

matrix&#91;&#40;2<<2&#41;+0&#93; = 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.
BlackDragon777
Posts: 32
Joined: Thu Sep 15, 2005 8:26 am

Post by BlackDragon777 »

Oh! Ok I see it now! Yeah I guess that would help a lot with readability. Thanks ooPo
Post Reply