Verifying a 3 bit end-around rotation in C++ [duplicate] - c++

This question already has answers here:
Best practices for circular shift (rotate) operations in C++
(16 answers)
Closed 9 years ago.
I need to do a 3 bit left end around rotation in C++.
So far I have:
A[i] = (A[i] << 3)|(A[i] >> 5);
A is an unsigned char array.
Is this correct? If not how can I fix it? Also what is the best way to test and see if this is correct?
Thanks

Looks fine to me.
If you really want to test it, work out a bunch of inputs and outputs by hand and check your program produces them.
Or devise another method that you're absolutely sure will produce results (eg convert unsigned char to binary string, rotate the string, convert back to unsigned char) and compare the two against all 256 possible inputs.

Related

Creating a bit sized variable [duplicate]

This question already has answers here:
Using Bit Fields to save memory
(2 answers)
When is it worthwhile to use bit fields?
(11 answers)
How slow are bit fields in C++
(3 answers)
C/C++: Force Bit Field Order and Alignment
(7 answers)
Declare a bit in C++
(5 answers)
Closed 2 years ago.
I wanted to create a bit sized variable to save the only possible values 2^1 = 0 | 1
My initial approach was to create a class with variables of type int for storing the value 0 | 1
But then i found that i could also use a bitfield and also create my own struct with custom bits for each type. My question is that does using a struct with bits set to 1 provide better memory performance and hence faster implementation than the class approach for a array of struct like ( 4000 x 4000 )
The code :
#include<iostream>
using namespace std;
struct maze
{
unsigned int top : 1;
unsigned int right : 1;
unsigned int bottom : 1;
unsigned int left : 1;
};
int main()
{
maze access;
cout<<sizeof(access);
access.top=1;
access.right=1;
access.bottom=1;
access.left=1;
cout<<endl<<sizeof(access);
return 0;
}
Edit:
I think i have found the answer : https://stackoverflow.com/a/46544000/13868755
This is really hard to speculate about the performance for this case, and there is a fair chance you'll make it work slower.
Unless this is a proven bottleneck, you should focus on writing a readable and testable code. Does the struct storing the four values make the code cleaner? If so, use the struct. Are the values actually boolean, i.e. true or false only? Use bool to make it clear to the reader.
This will be likely just as performant as your current implementation, and take as much space.
For gauging the performance you need a working code to benchmark on, as for the different applications the performance implications will differ, and guessing it beforehand, though a funny thought experiment, isn't really useful in practice.
An exception is when you know you have a lot of data (like, gigabytes) and your are memory constrained. In that case you should indeed focus on the memory over both code readability and CPU usage. In that case going straight to std::bitset would look like a promising option, with good memory usage guarantees and proven correctness.
If memory is only a secondary concern, simply using packed structs/arrays (look for compiler options for that) should be sufficient and way simpler and cleaner to write.

Why there is different types of pointers? [duplicate]

This question already has answers here:
Why are there different types of pointers for different data types in C?
(7 answers)
Closed 6 years ago.
First of all, I'm sorry if this question is stupid
and I'm beginner in C/C++
My question is:
Why when I want to point to a char variable like ( char x = 'A' ) I should make a pointer of char datatype? like this one ( char * pnt = &x ) ? I thought the address should be always integer of any place in Memory so the variable x in my example stored in RAM like this format (01000001) after converted (65) to the binary system in some address .. so there is an address of char type ??
I didn't understand the concept, any explanation?
That's because of strong typing in C/C++. That is one of the paradigms (python uses different one for example), but the only possible in C++. You actually can use your knowledge about pointer types and transform pointer between types. Use static_cast, dynamic_cast and so on to do this. Also if you are using C you can define a pointer to "something" - void*. This one can point to char, to int or to other type you can imagine. Notice: such transformation should be conscious. This can be a symphtom of a bad architecture or other troubles.

How to store a very large number in c++ [duplicate]

This question already has answers here:
How to store extremely large numbers?
(4 answers)
Closed 5 years ago.
Is there any way to store a 1000 digit number in c++? I tried storing it to an unsigned long double but it is still to large for its type.
You may find your answer here How to store extremely large numbers? GMP answer sounds right, ie this is what it does with pi digits https://gmplib.org/pi-with-gmp.html
You have to implement it yourself or use a library for it. In particular I love GMP: https://gmplib.org/ , which is an C implementation of Big Int/Float and has C++ wrapper
Use a custom class for your number, something like this:
#include <vector>
#include <iostream>
class large_num {
private:
int digits; // The number of digits in the large number
std::vector<int> num; // The array with digits of the number.
public:
// Implement the constructor, destructor, helper functions etc.
}
For a very large number just add each digit to the vector. For example if the number if 123456, then you do num.pushback(); In this case push all the digits 1,2, .. 6. You can store a extremely large numbers this way.
You should try this one
http://sourceforge.net/projects/libbigint/
great library for things like this.
also you can use boost.
http://www.boost.org/doc/libs/1_53_0/libs/multiprecision/doc/html/boost_multiprecision/intro.html
also one of the most commons is
https://gmplib.org/
Depends on the usage.If you need to do computation on it, probably go with the Big Int Library. If not and only aim is storing, store it in an array with each digit stored in one array element.

Which reason of using GL data types instead standart in C++? [duplicate]

This question already has answers here:
What's the advantage of using GLuint instead of unsigned int?
(3 answers)
Closed 8 years ago.
As I understand GLuint(data type in OpenGL) is exactly same as unsigned int. So, when it`s better use it?
Always use GLuint when you are going to use those variables in OpenGL calls. This is because it's possible that those types will not be unsigned ints in the future, and if your code uses unsigned int, it might break mysteriously in the future. This is true in general for any such library-specific datatypes.

How can I define a number with a valid range of -PI to PI? [duplicate]

This question already has answers here:
C: How to wrap a float to the interval [-pi, pi)
(15 answers)
Closed 9 years ago.
I'm wondering if it's possible to define a custom data type which can only take a value between -3.1415926535897932 and 3.1415926535897931.
The idea is that a value below or above the range would automatically "wrap-around", eliminating the need to write code to do the conversion and also eliminating the possibility of error somewhere.
Yes it is possible. In the method that sets the value check to see if it outside the limits and if so do whatever operation you want to do to force it to be inside the limits. fmod is one good choice for operation.