Combing 3 byte/pin values into one byte [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have 4 pin values defined P1_1, P1_2, P1_3, P1_4, with bit values. (1/0)
I want to combine them into one byte value, example:
0000 0101 (3 LSB are the pins)
How can i do this?

Something like
value = (P1_1<<3)|(P1_2<<2)|(P1_3<<1)|(P1_4);
Disclaimers: this is pretty ugly example. In your actual code you probably want some kind of constants defined for shift (so you are able to do the reverse operation without using more magic literal values).
Also note that this will have unexpected results if one of P constants is not 0 or 1. In this case one might use something like !!P1_x instead of P1_x.

bitset<4> temp;
temp[0] = P1_1;
temp[1] = P1_2;
temp[2] = P1_3;
temp[3] = P1_4;
unsigned char value = static_cast<unsigned char>(temp.to_ulong());

Related

C++ to Delphi convert. `this` pointer [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
In C++:
static_cast<unsigned int>(reinterpret_cast<unsigned int>(&this[-0x00003111]))
I don't understand &this[-0x00003111]
I can't understand "[-address]"
How to convert it to delphi code?
This is a strange piece of code. To understand this the operator precedences have to be considered.
However, the important missing piece is the class to which this refers to. I assume this happens in a method of class A. The following code does the same like
static_cast<unsigned int>(reinterpret_cast<unsigned int>(&this[-0x00003111]))
but with separated expressions:
A *pThis = this;
A &a = pThis[-0x00003111]; // aka. A &a = *(pThis - 0x00003111);
A *pA = &a;
unsigned int ui = reinterpret_cast<unsigned int>(pA);
unsigned int ui2 = static_cast<unsigned int>(ui);
(To get this compiling at all, I had to use -fpermissive.)
Considering that 64 bit CPUs are very common nowadays, may be, it is worth to mention that the unsigned int may have 32 bits only where the this pointer has 64 bit in this case. So, the reinterpret_cast clips half of the bits away in this case.
Life Demo on coliru
Dear children.
Please, don't do this at home.

Difference Between initializing an array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have been getting a weird output if I initialize an array using a variable instead of a constant
the two code below produces a different output for me.
int x =7;
int arr[x];
and
int arr[7];
the first one generates this output
78 9 73 32 6422216 50 42
and the 2nd one
78 9 73 54 29 50 42
I need to use the size of an vector for the array size.
I have tried making the variable constant but it doesn't makes a difference.
edit
using the array here
int arr[size];
for(int j=i;j<nums.size();j++)
arr[j+1]=nums[j];
arr[i]=nums[signs.size()];
for(int j=0;j<nums.size();j++)
nums[j]=arr[j];
In neither of your two cases is the array initialized.
Without an explicit initializer, the contents of an array (or any variable) are indeterminate. You can't predict what those values will be. And if one of those values happens to be a trap representation, you invoke undefined behavior if you attempt to read that value.
If you want your array to have a particular set of values to start, you need to set those values explicitly, either with an initializer or by assignment.

Reason for using bit manipulation in array size [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 5 years ago.
Improve this question
I have recently got a basic idea of bit manipulation, and I was going through a problem where I found this C++ statement:
int popcount[1<<16];
I do have a basic idea of left/right Bit shift, but I would like to know why it is used in array size place.
Unless you find a comment in the code and unless you find out what the intent of popcount is, one can just guess why one writes 1 << 16 instead of, for example, 65536.
A common case could be that you want to count the number of occurrences of a particular id in, for example, a file. If the range of such an id where 16 bits, then such code could look as follows. The [1<<16] then expresses that you expect a range of not more than 16 bits:
int popcounts[1<<16] = { 0 };
int main() {
uint16_t id;
while (myfile >> id) {
popcounts[id]++;
}
}
Note that this is more accurate than writing int popcounts[UINT_MAX], because UINT_MAX is guaranteed to be equal or greater than 65536, and it is not guaranteed to be exactly 65536.
1<<16 is a common way to write 2 ** 16, which is easier to verify and modify than the "magic number" 65536. You may also encounter things like 1000 * 1000 instead of 1000000 for the same reason (although C++14 allows for 1000'000).

What is the size of bool? 1 bit or 1 byte? [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
printf("bool: %zu; true: %zu; 1: %zu\n", sizeof (bool), sizeof true, sizeof 1);
The above piece of code yields
bool: 1; true: 1; 1: 4
I am fine with the sizeof(1), it is an integer and hence 4 bytes size.
But bool (and true) only needs a size of 1 "bit", still the output is 1 i.e. 1 byte.
Why is this so?
For an efficient (packed) representation use std::bitset:
#include <bitset>
std::bitset<2000000> my_bits;
Obviously this is for C++ only. In C you would have to implement this explicitly yourself, e.g.:
#include <stdint.h>
#include <limits.h>
uint8_t my_bits[2000000 / CHAR_BIT];
and then to access individual bits you would need to implement some simple inline functions using bitwise operations.
The minimum addressable unit is byte. Each object shall have its address. Also sizeof operator returns number of bytes occupied by an object. Even empty structures in C++ shall have non-zero sizes.
You can use standard class std::bitset instead of a boolean array.
An alternative to compile sized std::bitset<N> is to use the specialization std::vector<bool> which has a dynamic size. But care, it doesn't act as regular container as it provides proxy to access individual bit.

C++ program for power but without using pow function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm a newbee in C++ and I'm writing a C++ program that asks the user to input two integers and then it raises the first integer to the power specified by the second integer. For example, if the user enters 5 and 8, the result will be 5 subscript 8, i.e., number five will be raised to the eighth power. The program must not use any pre-defined C++ functions (like pow function) for this task. The program should allow the user to perform another calculation if they so desire. Can anyone help
I'm not going to give you any code, because that won't allow you to truly explore this concept. Rather, you should use this pseudo code to implement something on your own.
Create a function which accepts two inputs, the base and the exponent.
Now there are several ways to go about doing this. You can use efficient bit shifting, but let's start simple, shall we?
answer = base
i = 1
while i is less than or equal to exponent
answer = answer * base
return answer
Simply loop through multiplying the base by itself.
There are other ways that focus on efficiency. Look here to see something that you may want to attempt: are 2^n exponent calculations really less efficient than bit-shifts?
The program must not use any pre-defined C++ functions (like pow function) for this task
You can use some piece of c++ code like follows, to compute xy, without using any predefined function:
int x = 5;
int y = 3;
int result = 1;
for(int i = 0; i < y; ++i)
{
result *= x;
}
cout << result << endl;
Output:
125
See a working sample here.