• 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

Hwo to read bit and set same bit in different byte

22 posts in this topic

Hello,

I am using integers as bit containers representing bool values.
How do I make a fast instruction that reads some bit from one integer and sets its value to another.
Lets say: there are two 64 bit integers. One of them x has some bits set.
I want to rewrite every second bit from x to y (from beginning). How do I make that avoiding code similar to that:
[code]
y=0; //clear y
t=0;
for (int i=0;i<64;i+=2)
if (x & (CONTAINER_INT(1) << (i%ContainerSize)))
{
y |= CONTAINER_INT(1) << (t%ContainerSize);
t++;
}
[/code]

I mean I want to avoid if instruction.

Regards. Edited by Misery
0

Share this post


Link to post
Share on other sites
why? I assume you also do something else then, besides the copying of every second bit?

If you need to do it bit by bit, I don't think you have any choice but to check it bit by bit.

My code copies all of them from x at the same time, not caring what they are, and conserve all of the other bits from y

Edit: this if you also need to copy zeros

y = (y&0xAAAAAAAAAAAAAAAALL) | (x&0x5555555555555555LL); Edited by Olof Hedman
0

Share this post


Link to post
Share on other sites
[quote name='Olof Hedman' timestamp='1350390352' post='4990691']
why? I assume you also do something else then, besides the copying of every second bit?

If you need to do it bit by bit, I don't think you have any choice but to check it bit by bit.

My code copies all of them from x at the same time, not caring what they are, and conserve all of the other bits from y
[/quote]
Your code doesn't match his. The bit index for the source and the destination are different variables; the source bit index is the loop index i, but the destination index is t which is stepped by one and only stepped for on-bits inside the if-statement. The behavior of his code is effectively: count the number of on-bits in every second bit index, and set that many bits in the destination. If the source contains 5 on-bits when counting every second bit, then the first 5 bits in the destination is set.

Although I'd say that his code and description doesn't convey the same message, and neither are very clear. A full description of the actual problem will help a lot.
1

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1350390907' post='4990694']
Your code doesn't match his. The bit index for the source and the destination are different variables; the source bit index is the loop index i, but the destination index is t which is stepped by one and only stepped for on-bits inside the if-statement. The behavior of his code is effectively: count the number of on-bits in every second bit index, and set that many bits in the destination. If the source contains 5 on-bits when counting every second bit, then the first 5 bits in the destination is set.

Although I'd say that his code and description doesn't convey the same message, and neither are very clear. A full description of the actual problem will help a lot.
[/quote]

Ah right. I actually assumed the t inside the if was a typo, since putting it outside in the for loop matches the description in the post and title
1

Share this post


Link to post
Share on other sites
[quote name='Olof Hedman' timestamp='1350391321' post='4990695']
[quote name='Brother Bob' timestamp='1350390907' post='4990694']
Your code doesn't match his. The bit index for the source and the destination are different variables; the source bit index is the loop index i, but the destination index is t which is stepped by one and only stepped for on-bits inside the if-statement. The behavior of his code is effectively: count the number of on-bits in every second bit index, and set that many bits in the destination. If the source contains 5 on-bits when counting every second bit, then the first 5 bits in the destination is set.

Although I'd say that his code and description doesn't convey the same message, and neither are very clear. A full description of the actual problem will help a lot.
[/quote]

Ah right. I actually assumed the t inside the if was a typo, since putting it outside in the for loop matches the description in the post and title
[/quote]
Actually, putting t++ in the loop itself makes yet another interpretation of the problem: it copies bit index 2*n to bit index n, since i increases by two and t increases by one each loop in that case.

The exact details here are important and, as I said, we need to know the actual problem. So far we've had two different descriptions, and yet another interpretation of it :)
0

Share this post


Link to post
Share on other sites
[quote name='Misery' timestamp='1350388346' post='4990682']
[code]
y=0; //clear y
t=0;
for (int i=0;i<64;i+=2)
if (x & (CONTAINER_INT(1) << (i%ContainerSize)))
{
y |= CONTAINER_INT(1) << (t%ContainerSize);
t++;
}
[/code]
[/quote]

[code]y = (CONTAINER_INT(1)<<popcount(x & 0x555555555555555ul)) - 1;[/code]

EDIT: popcount stands for "population count", which means count the number of bits set. gcc provides __builtin_popcount for you, or you can use one of the tricks described in [url="http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive"]Bit Twiddling Hacks[/url]. Oh, and I am not sure why you are doing `%ContainerSize'. Edited by alvaro
0

Share this post


Link to post
Share on other sites
OK, so little more explanation:
I have a bitset matrix (B) and two indexing integer matrices (i,j).
What I want is to create another bitset matrix that consists of B entries at the indexes defined by i and j.
It is a similar behavior like that in Matlab or Scilab.

So what I need is to rewrite B(i,j) to new bitset matrix. B entries can be 1 or 0 so I need to rewrite them bit by bit, as new matrix can have different size from B.

To complicate even more used integer depend on the platform and may have 16 32 or 64 bits. This is dependent on the template parameter CONTAINER_INT.
0

Share this post


Link to post
Share on other sites
So your question has nothing to do with the code in your first post. :)

If I understand your problem correctly, I don't think you can do better than bit by bit.
1

Share this post


Link to post
Share on other sites
something like this?

Not sure if the logic is correct.

[code]y = 0;
t = 0;
while(x)
{
y |= ((x&1) << t);
t++;
x >>= 2;
}[/code] Edited by papalazaru
1

Share this post


Link to post
Share on other sites
You need to be much more precise than that. In your description of the problem you speak of "matrices" B, i and j, which are nowhere to be found in your code. You also don't explain what you mean by "matrix", because the conventional definition doesn't match either.

Once we have a precise statement of the problem, hopefully with some examples, I am pretty sure I'll be able to write some basic code to do what you need.
1

Share this post


Link to post
Share on other sites
[b]@Alvaro[/b]:
[quote name='alvaro' timestamp='1350431259' post='4990929']
You need to be much more precise than that. In your description of the problem you speak of "matrices" B, i and j, which are nowhere to be found in your code. You also don't explain what you mean by "matrix", because the conventional definition doesn't match either.

Once we have a precise statement of the problem, hopefully with some examples, I am pretty sure I'll be able to write some basic code to do what you need.
[/quote]

You're right. However, if I wanted to paste the whole code It would require about 10k lines of code at the moment, so I decided to strip it a little. And my question considers only:[b] How to rewrite one integer to another BIT BY BIT[/b]. That is the part I am stuck at. I just have no idea how to write an instruction that does this: [b]if the i-th bit in integer x is set, that set a j-th bit in integer y, if the i-th bit in x is unset, unset j-th bit in y.[/b] Also notice, that used integers are template parameters/arguments so no constant values should be used, rather constructors like CONTAINER_INT(1) or CONTAINER_INT(0).
Hope this clears the problem. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

PS.: And I want to do it bit by bit. I just want to avoid if statement.


EDIT: After giving it some thought, I paste the whole function (only the line with if statement has to be changed):
[code]
template <class INT_TYPE,class CONTAINER_INT>
inline void BitsetChooseSerial(CONTAINER_INT *From,INT_TYPE FromRows,INT_TYPE *WhichRows,INT_TYPE NRows,INT_TYPE *WhichCols,INT_TYPE NCols, CONTAINER_INT *To)
{

//ToRows==NRows
static const INT_TYPE ContainerSize=sizeof(CONTAINER_INT)*8;

INT_TYPE ElementIndex,C,i,t,tC;

for (INT_TYPE j=0;j<NCols;j++)
{

C=WhichCols[j]*FromRows;

tC=j*NRows;

for (i=0;i<NRows;i++)
{

ElementIndex=WhichRows[i]+C;

t=i+tC;

if (From[ElementIndex/ContainerSize] & (CONTAINER_INT(1) << (ElementIndex%ContainerSize))) To[t/ContainerSize] |= CONTAINER_INT(1) << (t%ContainerSize); //change this to direct bit shift

}
}
}
[/code] Edited by Misery
0

Share this post


Link to post
Share on other sites
[quote name='papalazaru' timestamp='1350414682' post='4990846']
something like this?

Not sure if the logic is correct.

[code]y = 0;
t = 0;
while(x)
{
y |= ((x&1) << t);
t++;
x >>= 2;
}[/code]
[/quote]

Unfortunately it doesnt work [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]
But thanks for help anyway[img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by Misery
0

Share this post


Link to post
Share on other sites
too bad shift operators are undefined with negative shifts, otherwise I'd know how :)
but if you know which index is the higher one, I think you can do something like this:

y = y&~(CONTANTER_INT(1)<<j) | (x&(CONTAINTER_INT(1)<<i)) >> (i - j))

possibly you can check which is higher outside the loop, and at least avoid the if in the loop.
1

Share this post


Link to post
Share on other sites
[b]@Olof Hedman[/b]: Actually I don't get the idea of this index stuff. Why would I need to find the bigger index value? Edited by Misery
0

Share this post


Link to post
Share on other sites
because if j > i in the above code, the shift will be negative, and negative shifts have undefined behaviour afaik Edited by Olof Hedman
1

Share this post


Link to post
Share on other sites
[b]@Olof Hedman: [/b]Yes, but why I would get negative shift after reading a bit value from x and placing this value at y?
0

Share this post


Link to post
Share on other sites
if you want to put bit 3 from x into bit 5 of y, that is i = 3, j = 5, then you have to shift the result of x&(CONTAINTER_INT(1)<<i) to the left, instead of right as the code does.

but to write a >> -2 is not allowed.
1

Share this post


Link to post
Share on other sites
@Olof Hedman: Oh, now i get it. But is there no way of doing it in the flow manner like: Read bit at some i in x, write this value to some j position of y? Without tangling indexes together?
0

Share this post


Link to post
Share on other sites
#define COPY_BIT(x, i, y, j) (y) = ((y) & ~(1 << (j))) | ((((x) >> (i)) & 1) << (j))

Something like that...? Can probably be optimized better. Edited by patrrr
1

Share this post


Link to post
Share on other sites
What patrrr wrote seems correct. I would do it with a function, though:
[code]unsigned copy_bit(unsigned x, int i, unsigned y, int j) {
y &= ~(1u << j); // Clear y's j-th bit
y |= ((x >> i) & 1u) << j; // Copy x's i-th bit into position j
return y;
}[/code]

The fact that you need to use some funky name for the type and that you are going for lots of generality is only confusing matters, so start without that.
1

Share this post


Link to post
Share on other sites
[b]@alvaro[/b] & [b]@patrrr[/b]: Thanks a lot Guys. That was exactly the thing I was looking for. [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]
alvaro: at the moment I cannot even count how many times you have halped me. Special thanks and huge respect to your skills!
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