The following statements in C:
iONE >>= iShift;
iONE &= 0xffefffff;
iONE |= (((((long)(*temp & 0x7f) - 65) << 2) + iShift + 1023) << 20) | (iTWO & 0x80000000);
Is there something like shr=, And= and Or= in Delphi. Is there more appropriate way than literal porting, especially the third statement.
Those are basically two operations combined.
x >>= y means x = x >> y.
The same applies for &= and |=.
And for the Delphi part: And, Or, and Right Shift.
Related
Using bitwise operations exclusively, how would I set y to 1 if all even-numbered bits of x are 1, and otherwise y is set to 0 (maximum of 8-bits)?
So far I have as follows:
p = ~x + 1
a = p >> 2
b = p >> 4
c = p >> 6
d = p >> 8
y = [insert code here]
Permitted: 12 operations (may use !, ~, +, -, <<, >>, &, ^, |) and up to 8-bit constants.
You could shift down all bits to the first bit and do a bitwise AND:
#even
y = 1 & (x >> 6) & (x >> 4) & (x >> 2) & x
#odd
y = (x >> 7) & (x >> 5) & (x >> 3) & (x >> 1)
Note the 1 & part for the even case since x >> 6 leaves the 2 MSbs in the result and you only want the low one.
Here's a demonstrative program in written in C:
Demo
Since you mentioned that you may use ! (NOT), a simpler version may be:
#even
!((x & 0b01010101) ^ 0b01010101)
#odd
!((x & 0b10101010) ^ 0b10101010)
Here the constants are first used to filter out the even/odd bits and then XOR with the same. This will produce 0 if all bits were set. Combine with NOT and you'll get 1 if they were all set and 0 if they were not.
Demo
I'm trying to replicate the function of a loop using only bitwise and certain operators including ! ~ & ^ | + << >>
int loop(int x) {
for (int i = 1; i < 32; i += 2)
if ((x & (1 << i)) == 0)
return 0;
return 1;
}
Im unsure however how to replicate the accumulating nature of a loop using just these operators. I understand shifting << >> will allow me to multiply and divide. However manipulation using ! ~ & ^ ~ has proven more difficult. Any Tips?
http://www.tutorialspoint.com/cprogramming/c_operators.htm
Edit:
I understand how the addition of bits can be achieved, however not how such an output can be achieved without first calling a while or for loop.
Maybe this can help:
int loop(int x) {
x = x & 0xaaaaaaaa; // Set all even numbered bits in x to zero
x = x ^ 0xaaaaaaaa; // If all odd numbered bits in x are 1, x becomes zero
x = !x; // The operation returns 1 if x is zero - otherwise 0
return x;
}
Your code tests all odd bits and returns 1 if all of them are set. You can use this bitmask: ...0101 0101 0101
Which, for 32 bits is 0xAAAAAAAA.
Then you take your value und bitwise-and it. If the result is the same as your mask, it means all bits are set.
int testOddBits(int x) {
return (x & 0xAAAAAAAA) == 0xAAAAAAAA;
}
I am doing bitwise & between two bit arrays saving the result in old_array and I want to get rid of the if/else statement. I should probably make use of the BIT_STATE macro, but how?
#define BYTE_POS(pos) (pos / CHAR_BIT)
#define BIT_POS(pos) (1 << (CHAR_BIT - 1 - (pos % CHAR_BIT)))
#define BIT_STATE(pos, state) (state << (CHAR_BIT - 1 - (pos % CHAR_BIT)))
if (((old_array[BYTE_POS(old_pos)] & BIT_POS(old_pos)) != 0) &&
((new_array[BYTE_POS(new_pos)] & BIT_POS(new_pos)) != 0))
{
old_array[BYTE_POS(old_pos)] |= BIT_POS(old_pos);
}
else
{
old_array[BYTE_POS(old_pos)] &= ~(BIT_POS(old_pos));
}
You can always calculate both results and then combine it. The biggest problem is to compute a fitting bitmask.
E.g.
const uint32_t a = 41,
uint32_t b = 8;
const uint32_t mask[2] = { 0, 0xffffffff };
const uint32_t result = (a&mask[condition])
| (b&mask[!condition]);
or to avoid the unary not
const uint32_t mask_a[2] = { 0, 0xffffffff },
mask_b[2] = { mask_a[1], mask_a[0] };
const uint32_t result = (a&mask_a[condition])
| (b&mask_b[condition]);
However: When doing bitwise manipulations, always be careful with the number of bits involved. One way to be careful is fixed size types like uint32_t, who may or may not be defined on your platform (but if not, the good thing is you get a compile error), or use templates carefully. Other types, including char, int and even bool can have any size beyond some defined minimum.
Yes, such code looks somewhat ugly.
I don't think BIT_STATE is useful here. (State MUST BE 0 or 1 to work as expected)
I see following approaches to get rid of them
a) Use C++ bitfields
For example
http://en.wikipedia.org/wiki/Bit_field
b)
"Hide" that code in a class/method/function
c)
I think this is equivalent to your code
if ((new_array[BYTE_POS(new_pos)] & BIT_POS(new_pos)) == 0))
{
old_array[BYTE_POS(old_pos)] &= ~(BIT_POS(old_pos));
}
or as inliner
old_array[BYTE_POS(old_pos)] &=
~((new_array[BYTE_POS(new_pos)] & BIT_POS(new_pos)) ? 0 : BIT_POS(old_pos));
Take the expression
(new_array[BYTE_POS(new_pos)] & BIT_POS(new_pos))
which is either 0 or has 1 in bit BIT_POS(new_pos) and shift it until the bit, if set is in BIT_POS( old_pos )
(new_array[BYTE_POS(new_pos)] & BIT_POS(new_pos)) << ( old_pos - new_pos )
now and the result with old_array[BYTE_POS(old_pos)]
old_array[BYTE_POS(old_pos)] &= old_array[BYTE_POS(old_pos)]
THe only trick is that it is implementation dependent (at least it used to be) what happens if you shift by a negative amount. So if you already know whether old_pos is greater or less than new_pos you can substitute >> ( new_pos - old_pos ) when appropriate.
I've not tried this out. I may have << and >> swapped.
What does the |= operator mean in C++?
Assuming you are using built-in operators on integers, or sanely overloaded operators for user-defined classes, these are the same:
a = a | b;
a |= b;
The '|=' symbol is the bitwise OR assignment operator. It computes the value of OR'ing the RHS ('b') with the LHS ('a') and assigns the result to 'a', but it only evaluates 'a' once while doing so.
The big advantage of the '|=' operator is when 'a' is itself a complex expression:
something[i].array[j]->bitfield |= 23;
vs:
something[i].array[i]->bitfield = something[i].array[j]->bitfield | 23;
Was that difference intentional or accidental?
...
Answer: deliberate - to show the advantage of the shorthand expression...the first of the complex expressions is actually equivalent to:
something[i].array[j]->bitfield = something[i].array[j]->bitfield | 23;
Similar comments apply to all of the compound assignment operators:
+= -= *= /= %=
&= |= ^=
<<= >>=
Any compound operator expression:
a XX= b
is equivalent to:
a = (a) XX (b);
except that a is evaluated just once. Note the parentheses here - it shows how the grouping works.
x |= y
same as
x = x | y
same as
x = x [BITWISE OR] y
It is a bitwise OR compound assignment.
In the same way as you can write x += y to mean x = x + y
you can write x |= y to mean x = x | y, which ORs together all the bits of x and y and then places the result in x.
Beware that it can be overloaded, but for basic types you should be ok :-)
What is the most efficient way to de-interleave bits from a 32 bit int? For this particular case, I'm only concerned about the odd bits, although I'm sure it's simple to generalize any solution to both sets.
For example, I want to convert 0b01000101 into 0b1011. What's the quickest way?
EDIT:
In this application, I can guarantee that the even bits are all zeros. Can I take advantage of that fact to improve speed or reduce space?
Given that you know that every other bit is 0 in your application, you can do it like this:
x = (x | (x >> 1)) & 0x33333333;
x = (x | (x >> 2)) & 0x0f0f0f0f;
x = (x | (x >> 4)) & 0x00ff00ff;
x = (x | (x >> 8)) & 0x0000ffff;
The first step looks like this:
0a0b0c0d0e0f0g0h0i0j0k0l0m0n0o0p x
| 00a0b0c0d0e0f0g0h0i0j0k0l0m0n0o0 x >> 1
--------------------------------
= 0aabbccddeeffgghhiijjkkllmmnnoop x | (x >> 1)
& 00110011001100110011001100110011 0x33333333
--------------------------------
= 00ab00cd00ef00gh00ij00kl00mn00op (x | (x >> 1)) & 0x33333333
Then the second step works with two bits at a time, and so on.
In terms of speed, a lookup table 16 bits wide with 2^32 entries will be hard to beat!
But if you don't have that much memory to spare, four lookups in a 256-entry table,
plus a few shifts and ANDs to stitch them together, might be a better choice. Or perhaps the sweet spot is somewhere in between...it depends on the resources you have available, and
how the cost of initializing the lookup table will be amortized over the number of lookups you need to perform.
I'm not sure how quick it would be, but you could do something like
int a = 0b01000101;
int b = 0;
int i = 0;
while (a > 0) {
b |= (a & 1) << i;
a >>= 2;
}
Which would pull all the odd bits off of a and put them on b.