Determine offset using only bitmask [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hello' I've got a problem
there is some telemetry values packed in words (or double words) and bitmask for each telemetry channel.
for example i've got mask 0xf000 and word value 0x2499
after & operation i've got 0x2000,but real telemetry value packed in this word is 0x2 (0x2000>>12). How can i determine this offset using only mask and without cycles like:
offset = 0;
for (int i = 0;i<32 i++)
{
if ((mask>>i)&1)
{
offset = i
break;
}
}

I think you may use some built in functions to get the first non-zero bit index.For example GCC has int __builtin_ffs (int x)
— Built-in Function: int __builtin_ffs (int x)
Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.
So in your case:
offset = __builtin_ffs(mask);
MSDN has similar intrinsics _BitScanForward and _BitScanForward64
https://msdn.microsoft.com/en-us/library/wfd9z0bb.aspx
All these builtin functions use special assembly instruction which performs the required calculation on the hardware level.

Maybe you can look at intrinsics to perform a "scan". The instruction "int _bit_scan_forward(int)" should do the trick ;)
https://software.intel.com/sites/landingpage/IntrinsicsGuide/#cats=Bit%20Manipulation

Related

Does C++ memset only works for 0 and -1? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
int a[2];
memset(a, 3, sizeof(a));
when I run this I am getting output as 0 1. Why not 3 3
Does C++ memset only works for 0 and -1?
It does work for all byte values.
int a[2];
memset(a, 3, sizeof(a));
when I run this I am getting output as 0 1.
I doubt that. Either your system is broken, or you made a mistake.
Why not 3 3
Because that's not what std::memset does. It sets every byte to the value that you provide. A multi-byte integer whose each byte have the value 3 doesn't have the value 3. In the value 3, only the least significant byte would have the value 3 and the more significant bytes would be 0.
Unless you want a repeating byte pattern, std::memset won't be useful to you. If you simply want to assign a value to each element, then you should be using std::fill or its friends:
std::fill(std::begin(a), std::end(a), 3);
Or just a bare loop:
for(int& i : a)
i = 3;
0 happens to be the only byte pattern that preserves the value across all byte widths on all systems; -1 is another but only on 2's complement systems. On 1's complement systems the other pattern has the value -0. This is why std::memset incidentally behaves the same as std::fill when using those values and only when using those values.

What is different between typedef with invariant and nomal typedef [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I came Across a line of code in an example that I can not understand how is it being utilized and what benefit on using this type of declaration.
following in the part of code extracted from that example
const uint16_t cycles = 8;
typedef uint8_t invariant(value < cycles) TIndex_t;
struct TData
{
uint16_t readings[cycles];
volatile uint16_t sum;
TIndex_t index;
void addReading(uint16_t arg)
writes(*this; volatile)
pre(invar())
pre(arg <= 1023)
post(invar())
{
sum = sum - readings[index] + arg;
readings[index] = arg;
index = static_cast<TIndex_t>((index + 1u) % cycles);
}
void init()
writes(*this; volatile)
post(invar());
ghost(
bool invar() const
returns((forall r in readings :- r <= 1023) && sum == + over readings);
)
This is GNU C++ the struct is a kind of class definition as I can tell
This is not pure C++.
It looks like somebody has written either a compiler extension or a series of macros to allow the specification of pre- and post-conditions for a block of code.
You're going to have to ask that person.
(As an aside, this is why just "extracting code" is often insufficient for understanding; you need to observe the full context in order to know what's going on. In this case, the context includes definitions and/or documentation found elsewhere and not included in your question.)

What is the real powerful use of shift operators in C ? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
One purpose of left shift operator is multiply the left Operand with 2 and right shift for integer Division. Both have also constraints like undefined behaviours Link etc. I do not understand what will be the real use. I am even not confident to use them for Division or multiplication.
Only beginners would ever use a shift for division or multiplication (including people who are writing software for decades and are still beginners).
Shift operations are for shifting bits. When you want to shift bits you use them. If you don't know what shifting bits means, you don't want to use them.
They're bitwise operators, so they're used for bitwise operations. Here's a trivial example that shows some bitwise operations that call for bit shifting.
struct color {
unsigned char r;
unsigned char g;
unsigned char b;
};
void setColor(struct color*,int);
int getColor(struct color*);
int main() {
struct color myColor;
int color;
setColor(&myColor,0x00ff00);
color = getColor(&myColor);
return 0;
}
void setColor(struct color* color,int rgb) {
color->r = (rgb>>16)&0xff;
color->g = (rgb>>8)&0xff;
color->b = rgb&0xff;
}
int getColor(struct color* color) {
return color->r<<16|color->g<<8|color->b;
}
One real-world use case is in manipulating bitmasks. For example, if I have a bitmask x and I want to set the 7th bit from the right (where I start counting at zero), I could do the following.
x = x | (1 << 7);

Create an overflow at a given value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
If a unsigned byte overflows it goes from 255 to 0 and vica versa -1 gives 255.
Would it be possible to have it overflow at for example 200?
Without using if statements.
Overflow is fairly simple:
unsigned int a = 150, b = 150;
a += b; // do an operation
a %= 200; // wrap it
However, with underflow, it's a bit harder (see orlp's answer for this).
To make it less error prone if you use this variable several times, in C++ with operator overloading, you can make a class that simulates an integer type which wraps after every operation with operator overloading.
The modulo operator does what you want, with some trickery for negative values:
int wrap(int x, int n) {
return x < 0 ? ((x % n) + n) % n : x % n;
}
// wrap(205, 200) == 5
// wrap(-1, 200) == 199
Unless your willing to learn assembly, such an action would be impossible for several reasons.
All the types like char, short, int, etc. are builtin and predefined by the parser.
200 isnt a power of two; computer represent numbers in binary.
Note: The above is only true if you want implicit overflow; modulas lets you do explicit overflow.

difference between literal types and variable types in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was studying C++ via cplusplus.com and came across something like 75u, which seems to describe an unsigned constant.
What has got me confused is: what's the point of declaring a constant to be unsigned when there is already a provision to declare the variable to which 75 will be assigned as unsigned?
Simpler said:
Why would you specifically add a u to a number when assigning it to (for example) an unsigned int?
What's the difference between
unsigned int i = 75;
and
unsigned int i = 75u;
That's because the type of the variable (in an assignment) on the left hand side of the = has nothing to do with how an expression is evaluated (the right hand side).
This seems to surprise many new programmers, but it's still true.
Something like this:
const float two_thirds = 2 / 3; /* Bad code! */
does not assign 0.6666667 to two_thirds; since both 2 and 3 are int literals, the expression is evaluated using integer math.
You need:
const float two_thirds = 2.f / 3;
to force the expression to float. Similar reasoning applies to the use of unsigned, since it has larger range than signed variables.