• 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
musafir2007

Simple bit masking operations

7 posts in this topic

Hi all,
So i am a little weak in all this bits stuff..

So far I realized that if I want to see if a particular bit is set...
for variable n, to see if kth bit is set, I will
unsigned n = ...
if ((1 << k) & n)
bit set...
else
not set...

Now for example I have a variable n, and I want to set its 10th bit to 1, how do I do that? Will I need to check first, or can I just force it to be 1 in a single expression?

thanks
1

Share this post


Link to post
Share on other sites
These methods count bits from most significant bits to least significant bits.
The set methods use pointers because they have side effects. Just place & in front of the variable to give it's address as a pointer.
The PowerOfTwo array only exist because all methods for integer exponents in C++ are ugly hacks and we only need 16 values.
[code]
const UINT16 PowerOfTwo[16] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
bool GetBitFromUINT8(unsigned char X, int Index) {
if (Index < 0 || Index > 7) {
printf("GetBitFromUINT8: Index is out of bound.");
return false;
}
return (X & PowerOfTwo[7 - Index]) > 0;
}

bool GetBitFromUINT16(UINT16 X, int Index) {
if (Index < 0 || Index > 15) {
printf("GetBitFromUINT16: Index is out of bound.");
return false;
}
return (X & PowerOfTwo[15 - Index]) > 0;
}

void SetBitInUINT8(unsigned char* pX, int Index, bool NewValue) {
if (Index < 0 || Index > 7) {
printf("SetBitInUINT8: Index is out of bound.");
} else {
if (NewValue) {
*pX = *pX | PowerOfTwo[7 - Index];
} else {
*pX = *pX & ~(PowerOfTwo[7 - Index]);
}
}
}

void SetBitInUINT16(UINT16* pX, int Index, bool NewValue) {
if (Index < 0 || Index > 15) {
printf("SetBitInUINT16: Index is out of bound.");
} else {
if (NewValue) {
*pX = *pX | PowerOfTwo[15 - Index];
} else {
*pX = *pX & ~(PowerOfTwo[15 - Index]);
}
}
}
[/code]
-2

Share this post


Link to post
[url="http://www.eskimo.com/~scs/cclass/int/sx4ab.html"]This write up[/url] probably explains it about as well as I ever could, and it takes me a hell of a lot less time to simply post a link. :)
1

Share this post


Link to post
Share on other sites
[quote name='musafir2007' timestamp='1319041716' post='4874366']
Now for example I have a variable n, and I want to set its 10th bit to 1, how do I do that?[/quote][font="Courier New"]
n |= 1 << 9; // set
n ^= 1 << 9; // toggle
n = n & ~(1 << 9); // clear
[/font]

EDIT:
Serapth's link's good too. :D
0

Share this post


Link to post
Share on other sites
Oh, and if this is for actual production code and not as a learning exercise, you should probably use [url="http://en.cppreference.com/w/cpp/utility/bitset"]std::bitset[/url].


[code]
long bitValues = 0x3B9ACA00; // 1000000000 in hex
std::bitset<10> myBits(bitValues);
bool isTenthBitSet = myBits.test(9);


or


std::bitset<10> myBits("1000000000",0,9);
bool isTenthBitSet = myBits.test(9);
[/code]




Not, I assumed everything was 0 based, but I would verify.
0

Share this post


Link to post
Share on other sites
Thanks for your replies. So basically I have..

func( params...)
{
useScaling = n & 1<<10;

if(useScaling)
{
//scaling algorithm
}
}

So now if I do want to do scaling..
would I do

n |= 1 << 9; // set
or
n |= 1 << 10; // set

func(n);

?

Thanks
0

Share this post


Link to post
Share on other sites
[quote name='musafir2007' timestamp='1319057520' post='4874449']
Thanks for your replies. So basically I have..

func( params...)
{
useScaling = n & 1<<10;

if(useScaling)
{
//scaling algorithm
}
}

So now if I do want to do scaling..
would I do

n |= 1 << 9; // set
or
n |= 1 << 10; // set

func(n);

?

Thanks
[/quote]

Realize, when you say the "10th" bit, since bit 0 is "1st" bit, then bit 9 would be the "10th". But, it doesn't really matter as long as you use the same shift operator to set and to get.

Also, Be careful with operator precedence. If you're not sure what gets executed 1st, you should use (). Since I'm not 100% sure, I would change the line to be this:
[code]
useScaling = n & (1<<10);
[/code]

Could turn out to be useScaling = (n & 1) << 10 (even though it isn't, it's good practice to use parenthesis for multiple operators)

So, to Set a bit for scaling (and you are using the 11th bit, which is bit 10), and check a bit, you'd do this:
[code]
// set for scaling
BitFlag |= (1 << 10);

// Check if we're using scaling
useScaling = BitFlag & (1 << 10);

if (useScaling)...
[/code]
0

Share this post


Link to post
Share on other sites
Hello, I am still a little bit confused. Here's my issue

[code]

func( int x, int y, unsigned char tIndex )
{
unsigned char tMask = 0;

if (x > 0 && y > 0)
{
tMask |= (1<<tIndex); //set a bit
}
else
{
tMask &= ~(1<<tIndex);
}


if ( tMask & tIndex)
{
//should be here if x and y were greater than 0 right???
}
else
{
//but comes here :(
}
}
[/code]

What am I doing wrong here and how can I fix it? thanks!
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