Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

Can someone please explain how this operator works in this case?

This topic is 3127 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How does the | work in the switch statement? I can see that the bitwise left shift is multiplying the row by 16 and have made a note so I don't forget! :) Unfortunately I'm not sure how the col integer is added to the shifted row integer using the | operator.
        public static float Get(ref Matrix matrix, int row, int col)
        {
            // << = bitwise shift left 
            // << 4 in BINARY shifts integer n from [n * 2^0] to [n * 2^4] which is [n * 16] 
            switch (row << 4 | col)
            {
                case 0x00: { return matrix.M11; }   // Hexadecimal values must start with "0x"
                case 0x01: { return matrix.M12; }
                case 0x02: { return matrix.M13; }
                case 0x03: { return matrix.M14; }

                case 0x10: { return matrix.M21; }   // 16
                case 0x11: { return matrix.M22; }   // 17
                case 0x12: { return matrix.M23; }   // 18
                case 0x13: { return matrix.M24; }   // 19

                case 0x20: { return matrix.M31; }   // 32
                case 0x21: { return matrix.M32; }
                case 0x22: { return matrix.M33; }
                case 0x23: { return matrix.M34; }

                case 0x30: { return matrix.M41; }   // 48
                case 0x31: { return matrix.M42; }
                case 0x32: { return matrix.M43; }
                case 0x33: { return matrix.M44; }


Share this post


Link to post
Share on other sites
Advertisement
"|" in this context is bitwise OR.

Example:
Let row = 3 = 00000011,
col = 2 = 00000010

row << 4 = 00110000
| col = 00000010
---------------------
result = 00110010 = 0x32

If you can assume that row and col are in the range 0 - 15 inclusive, the 4 lower bits of row << 4 will be 0's, so the operation is equivalent to adding col to the shifted row value.

Share this post


Link to post
Share on other sites
Quote:
Original post by mattd
If you can assume that row and col are in the range 0 - 15 inclusive, the 4 lower bits of row << 4 will be 0's, so the operation is equivalent to adding col to the shifted row value.
In other words, row << 4 | col is equivalent to row * 16 + col (neglecting any possible overflow).

However, this is an interesting example of premature optimisation, because even at the lowest optimisation level, GCC transforms row * 16 + col into a shift + add, and at -O3 row * 16 + col is rendered in fewer instructions than row << 4 | col.

Here is my test file, incase you want to check for yourself:

#include <stdio.h>

int get(int row, int col) {
return row << 4 | col;
}

int main() {
int i = get(5, 4);
printf("%d", i);
}

Share this post


Link to post
Share on other sites
So I guess the bitwise OR operator is used as it is faster than using the + operator?

EDIT: swiftcoder answered my question before I posted it! :D

EDIT2: So now my switch statement reads:

switch ((row << 4) + col)

Thanks, both of you.

[Edited by - Spa8nky on November 2, 2009 8:13:59 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!