Public Group

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

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

## 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 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 on other sites
"|" 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 on other sites
Quote:
 Original post by mattdIf 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 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]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634134
• Total Posts
3015750
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!