Skip to main content
GameDev.net gamedev.net
🔒 Locked

Hwo to read bit and set same bit in different byte

Started by Misery Oct 16, 2012 at 11:52 AM 21 replies 4.2k views
Original Post
Misery
Misery
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:

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++;
}


I mean I want to avoid if instruction.

Regards.
alh420
alh420
something like this?

y |= x&0x5555555555555555LL;
Misery
Misery
@Olof Hedman: No. I need to rewrite them bit by bit.
alh420
alh420
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);
Brother Bob
Brother Bob

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

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.
alh420
alh420

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.


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
Brother Bob
Brother Bob

[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.


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
alvaro
alvaro


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++;
}



y = (CONTAINER_INT(1)<<popcount(x & 0x555555555555555ul)) - 1;

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 Bit Twiddling Hacks. Oh, and I am not sure why you are doing `%ContainerSize'.
Misery
Misery
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.
alvaro
alvaro
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.
oliii
oliii
something like this?

Not sure if the logic is correct.

y = 0;
t = 0;
while(x)
{
y |= ((x&1) << t);
t++;
x >>= 2;
}
Everything is better with Metal.
alvaro
alvaro
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.
Misery
Misery
@Alvaro:

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.


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: How to rewrite one integer to another BIT BY BIT. That is the part I am stuck at. I just have no idea how to write an instruction that does this: 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. 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. smile.png

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):

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+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

}
}
}
Misery
Misery

something like this?

Not sure if the logic is correct.

y = 0;
t = 0;
while(x)
{
y |= ((x&1) << t);
t++;
x >>= 2;
}



Unfortunately it doesnt work sad.png
But thanks for help anywaysmile.png
alh420
alh420
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)<> (i - j))

possibly you can check which is higher outside the loop, and at least avoid the if in the loop.
Misery
Misery
@Olof Hedman: Actually I don't get the idea of this index stuff. Why would I need to find the bigger index value?
alh420
alh420
because if j > i in the above code, the shift will be negative, and negative shifts have undefined behaviour afaik
Misery
Misery
@Olof Hedman: Yes, but why I would get negative shift after reading a bit value from x and placing this value at y?
alh420
alh420
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)<
but to write a >> -2 is not allowed.
Misery
Misery
@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?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.