Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075929 Posts in 44152 Topics- by 36119 Members - Latest Member: Royalhandstudios

December 29, 2014, 04:03:05 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Finding the number of coincident bits
Pages: [1]
Print
Author Topic: Finding the number of coincident bits  (Read 676 times)
Quarry
Level 10
*****



View Profile WWW
« on: September 24, 2013, 08:04:14 AM »

Let's say I have a 5 bit number like 10010

I shift it by 2 to the right and OR it onto a byte so it become 00000100

How can I know the amount of bits that were "relevant" on the byte which is 3 in this case

I also need a solution that works when I shift it to the left! How can I go on about this?
Logged

 
Columbo
Level 0
***


View Profile
« Reply #1 on: September 24, 2013, 08:55:54 AM »

It's possible I've misunderstood the question, but I think the magic phrases to google are "Count leading zeros", "Count trailing zeros", "Find Most Significant Bit".

The solution you choose will depend on the hardware you're targeting and how much you enjoy the amazing bit twiddling hacks that are around. I love this page http://graphics.stanford.edu/~seander/bithacks.html, but it's usually pretty hard to justify using the tricks because of the readability cost from being too clever by half.
Logged

Gregg Williams
Level 10
*****


Retromite code daemon


View Profile WWW Email
« Reply #2 on: September 24, 2013, 10:59:58 AM »

I guess the worse case scenario is 31 mask tests for a 32bit number, I'm sure there are cleverer ways though.
Logged

ThemsAllTook
Moderator
Level 10
******


Alex Diener


View Profile WWW
« Reply #3 on: September 24, 2013, 11:28:37 AM »


+1 for this. Really great resource. For the "too clever by half" concern, I usually reference the URL in a comment next to the function I use from the page, just in case I come back later and can't remember what's going on.
Logged
muki
Level 1
*



View Profile
« Reply #4 on: September 25, 2013, 05:49:44 AM »

I find it really interesting that bitshifts and bit hacks in general are still used for game dev, even now that we have "unlimited processing power"!

I come from an embedded background. The only programming I did was for microcontrollers where bit manipulation was commonplace because we had to.  Smiley I haven't really done any game programming yet (except for some gamemaker) but it's nice to see it hasn't died!
Logged
tbkn
Level 0
*


View Profile
« Reply #5 on: September 25, 2013, 09:47:40 AM »

Logarithm will be your answer.
Do a logarithm to the byte, add 1, and then round it down to the nearest int.

So, if you have 5 (101), then log2(5) will be 2.XXX
Add one and you get 3.XXX
round it down and you get 3.

thats because log2 of any number that has only one "1" bit, will be the location of that bit (0 being the first), so anything between 100 and 1000 will be between 2 and 3 for example.
Logged
Quarry
Level 10
*****



View Profile WWW
« Reply #6 on: September 25, 2013, 10:04:24 AM »

I think I failed to explain myself but I solved the issue with a very stupidly inefficient snippet

Code:
public static final int getSignificantBitCount(int shift) {
if (LOOKUP_BITS + shift > 8) {
return LOOKUP_BITS + (8 - (LOOKUP_BITS + shift));
} else if (shift < 0) {
return LOOKUP_BITS + shift;
} else {
return LOOKUP_BITS;
}
}
Logged

 
Evan Balster
Level 10
*****


I live in this head.


View Profile WWW Email
« Reply #7 on: September 25, 2013, 10:05:29 AM »

The quickest way to do it would be this one:

Code:
unsigned int v;         // 32-bit value to find the log2 of
register unsigned int r; // result of log2(v) will go here
register unsigned int shift;

r =     (v > 0xFFFF) << 4; v >>= r;
shift = (v > 0xFF  ) << 3; v >>= shift; r |= shift;
shift = (v > 0xF   ) << 2; v >>= shift; r |= shift;
shift = (v > 0x3   ) << 1; v >>= shift; r |= shift;
                                        r |= (v >> 1);

Of course, I doubt this kind of performance is necessary unless you're doing some kind of DSP.
Logged

Creativity births expression.  Curiosity births exploration.
Our work is as soil to these seeds; our art is what grows from them...


Wreath, SoundSelf, Infinite Blank, Cave Story+, <plaid/audio>
ThemsAllTook
Moderator
Level 10
******


Alex Diener


View Profile WWW
« Reply #8 on: September 25, 2013, 12:04:48 PM »

I find it really interesting that bitshifts and bit hacks in general are still used for game dev, even now that we have "unlimited processing power"!

Extra processing power opens up a lot of doors beyond writing less efficient code. It's kind of a pet peeve of mine that software tends to get slower at the same pace that hardware gets faster, so nothing ever gets any more responsive from the user's perspective. Some examples of good usage of extra CPU cycles/RAM: Braid's rewind mechanic, Minecraft's huge world of cellular automata. I'm working on a game with online leaderboards, which verifies submitted scores by running a full simulation of the user's play session based on their inputs. For this to work, my game simulation has to be fast enough to verify a full play session in a tiny fraction of how long it took in real time - specifically, a fraction equal to 1 / the maximum number of players submitting scores at once.

Aside from all that, bit twiddling is sometimes more expressive than other, more expensive ways of computing things.
Logged
Quarry
Level 10
*****



View Profile WWW
« Reply #9 on: September 25, 2013, 12:12:54 PM »

Quote
verifies submitted scores by running a full simulation of the user's play session based on their inputs

Is that overkill or not? It seems like you are trying to do your best to prevent cheating
Logged

 
ThemsAllTook
Moderator
Level 10
******


Alex Diener


View Profile WWW
« Reply #10 on: September 25, 2013, 01:13:35 PM »

Well, "verifies" is the wrong word. There's no communication of what the client thinks the player's score is; a score submission consists of a set of inputs and a player ID, and the server computes the score on its own by simulating the play session. I've played a couple of games lately where I wished the leaderboards would let me replay the game sessions of the people above me (specifically for speedruns, so I could see the route they took). I wanted to have this feature in the game I'm working on now, and having the score calculated by the server by resimulating it just seemed like a logical extension of that. Preventing cheating is certainly a nice side effect; the only cheats that will be possible are edited inputs or tool-assisted gameplay, and I'm OK with that.
Logged
Gregg Williams
Level 10
*****


Retromite code daemon


View Profile WWW Email
« Reply #11 on: September 25, 2013, 01:14:49 PM »

Networking still has plenty of love for bits and their various uses.
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic