c++ binary value for indexed array [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
I am extremely new to C++ so I'm probably asking a very trivial question, but if you could help that'd be great!
I have an array[n].
Indexed from 0 to some unknown value.
I need to access the index of the array, the n value but I need to do so in binary. I am intending to do a bit reversal on it.
So, if I have an array of 2048 points how do I represent the 1024 array in binary?

If you want to write a value in binary, you can do so in C++14 with
int my_binary_value = 0b01010101;
If you'd like to test a specific bit of an int, you can do that by masking it, i.e.
bool is_bit_4_set = my_binary_value & 0b00001000;

Related

Is there any optimal way to implement N byte integer and it's arithmetic operations in c++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
I'm trying to think of some interesting, reusable way to implement big integers using passed amount of bytes or resizing themselves when needed. I have no idea how to make it optimal in any way tho. Are there any tricks I could use, or do I have to simply work on those numbers bit by bit while adding/multiplying/dividing?
edit: if it is important, I need it to safe text as number in base 10 so I can play with some ideas for encrypting it
Use The GNU Multiple Precision Arithmetic Library. If you try to reinvent the wheel you will end up with a square.

Taking input using delimiter 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 2 years ago.
Improve this question
After taking those values I have to store the values in two different arrays (say x and y)
So the only error I can immediately see is this
float* a = NULL;
a = new(nothrow)float;
which should be
float* a = new float[count];
Your version only allocates enough space for a single float when you really need space for count floats.
When you have code that isn't working, and you want to ask a question about it you really should say exactly what happens when you run the code. Doing this will help get you better answers.

Appending bits to increase the size of a char [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
Is there any way I can append the size of a particular variable? For instance, if I have:
static_cast<char>(0x0147)
an 8 bit char and I want to increase it to say 16 bits without changing the data type, is that possible?
No.
The size of the object is not only related to its type; the size is defined by the type.
You cannot change one and not the other.
Just initialize a new int16_t from this char if that's what you want.
Or, you could have a vector<char> and add new elements to this collection as needed.
(Shifting has nothing to do with it; that's about transforming data.)

What is Predecessor of an element in an array? How do I find it? [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 tried to Google but in vain.
In C++ arrays are stored in contiguous memory.
This means that if you have an element x and you know that it's inside an array and that it's not the first element of the array, then then previous one is
*(&x - 1)
i.e. the element pointed by the address of x after we subtract 1 (note that this works because pointer arithmetic in C++ considers element size, so &x - 1 is not point to the byte before, but to the element before).

can 0-1 knapsack be implemented using 1D array? [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 9 years ago.
Improve this question
I found implementation using 2D array http://www.geeksforgeeks.org/dynamic-programming-set-10-0-1-knapsack-problem/. But how to implement it using 1D array and if it is not possible then why?
Note that each of values in the current row uses only elements of the current and previous rows. Hence you can implement the algorithm with an array K[2][W], which is the same as using an array K[2*W] with some additional trivial index calculations