• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Misery

Smart way to count positive entries in bitset

13 posts in this topic

Hello,

I have an array of integers (let us assume here 32 bit ints).

[code]
N=SomeValue;
int *ptr=new int[N];
[/code]

Does there exist some clever way to sum all the ones (positive bits) instead of going trough every bit and converting it to bool, and if true then sum++?
0

Share this post


Link to post
Share on other sites
Pity :(
I thought that it could work similarily to filling with ones or zeros the whole bitset. It is enough then to set ~0 or 0 to the whole container.
0

Share this post


Link to post
Share on other sites
Written in Notepad, it may or may not compile.
Also it's not tested, it may or may not be faster/slower, that's up to you to test.

[CODE]
// initialize table, could be constant, but that's a lot to type out
int numBits[256] = { };
for(int i = 0; i < 256; ++i) {
numBits[i] =
((i & 0x01) ? 1 : 0) +
((i & 0x02) ? 1 : 0) +
((i & 0x04) ? 1 : 0) +
((i & 0x08) ? 1 : 0) +
((i & 0xF0) ? 1 : 0) +
((i & 0xF1) ? 1 : 0) +
((i & 0xF2) ? 1 : 0) +
((i & 0xF4) ? 1 : 0) +
((i & 0xF8) ? 1 : 0);
}

int *ptr=new int[N];
int totalNumBits;
for(int i = 0; i < N; ++i) {
union {
int integer;
char charArray[4];
} int2char;
int2char.integer = ptr[i];
totalNumBits += numBits[int2char.charArray[0]] + numBits[int2char.charArray[1]] + numBits[int2char.charArray[2]] + numBits[int2char.charArray[3]];
}
[/CODE]
0

Share this post


Link to post
Share on other sites
I don't understand the question quite well, but if you are talking about count bit number in an integer, or any similar bit algorithm, here is a very good bit algorithm collection.
http://graphics.stanford.edu/~seander/bithacks.html
Bookmark/Delicious it if you didn't. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
2

Share this post


Link to post
Share on other sites
[quote name='Ripiz' timestamp='1349433633' post='4987071']
Written in Notepad, it may or may not compile.
Also it's not tested, it may or may not be faster/slower, that's up to you to test.
[/quote]

It's also incorrect, you would need to test against:

0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80.
2

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1349445503' post='4987115']
gcc also has a function called __builtin_popcount, but on my computer the one above is three times faster. You can do it even faster using 64-bit arithmetic.
[/quote]
Talking about performance, here is my personal experience (maybe off topic).
There is another lookup table algorithm for bit counting in my posted url, but be careful to use it.
AFAIR, it may be slower due to the data lookup may invalid the cpu data cache.
The algorithm posted by alvaro should be better because it uses "in-place" data and doesn't access any data in the memory. Edited by wqking
1

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1349445503' post='4987115']
This code (or similar) is somewhere in that bithacks page wqking linked:
[code]unsigned popcount(unsigned x) {
x -= (x >> 1) & 0x55555555u;
x = (x & 0x33333333u) + ((x >> 2) & 0x33333333u);
x = (x + (x >> 4)) & 0x0f0f0f0fu;
return (x * 0x01010101u) >> 24; // assumes unsigned is 32 bits
}
[/code]
[/quote]
That function scares me... :)
0

Share this post


Link to post
Share on other sites
[quote name='luca-deltodesco' timestamp='1349439130' post='4987085']
[quote name='Ripiz' timestamp='1349433633' post='4987071']
Written in Notepad, it may or may not compile.
Also it's not tested, it may or may not be faster/slower, that's up to you to test.
[/quote]

It's also incorrect, you would need to test against:

0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80.
[/quote]

Oh my... What have I done. Sometimes I wish I could downvote myself.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0