Shifting binary number in c++ [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 2 months ago.
Improve this question
For example I want to do:
I have 1 it is 00000001 in binary.
and when I shift like 1<<3 I'll take 8 it is 00001000.
So, I need that I take 00001111 after 1<<3.
I wish you understand if something wrong add unclear ask about it please.
I want to do shorter this part:
for(int i=1;h>0;h--,i*=2) hr+=i;

As I understand, you want
std::uint32_t my_shift(std::uint32_t n, std::uint32_t lshift)
{
return (n << lshift) | ((1 << lshift) - 1);
// original shifted | 0b0001111 (lshift 1)
}

You can directly iterate hr:
for(int hr=1; h>0; h--, hr=2*hr+1)

Related

Can someone explain please what does this do : n&1? [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 5 years ago.
Improve this question
Here is the code, I know what does it do , but I don't understand, what does the if condition do?
if(n&1)
{
for(i=n/2,j=n/2+1;;i--,j++)
if(__gcd(i,j)==1)
{
cout<<i<<" "<<j<<endl;
break;
}
}
else
{
for(i=n/2-1,j=n/2+1;;i--,j++)
if(__gcd(i,j)==1)
{
cout<<i<<" "<<j<<endl;
break;
}
}
return 0;
}
Its a bitwise operator. There's a search term for you!
The & operator provides a mask that "cancels out" bits in the first depending if they're set in the second parameter - so assume N is the number 17, that expressed in binary is 00010001, the number 1 in binary is 00000001, so masking the two together will "blank" the first set of bits, leaving you with N as 00000001.
Basically that particular if statement drops all except the last bit, which is either 0 or 1, so it is a condition detecting if N is odd or even.

Reading int values from a vector 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
I have a problem to read a large int ( 1 to 10^100 ) into vector the problem is I cannot read it as numeric data-type and split it into the vector so I want a solution to read the number separately into the vector
Example:
45686469
vec[0] = 4
vec[1] = 5
...
vec[7] = 9
Here's one possible way to do it:
std::string yourinput;
cin>>yourinput; //capture your large number as a string
std::vector<char> vch;
for(size_t st=0;st<yourinput.length();++st)
{
vch.push_back(yourinput[st]); //move each character into the vector
}

Increment a alpha numerical string abcd1234 upto abcd2000 in c or 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
Increment a alpha numerical string "abcd1234" upto "abcd2000"
output:
abcd1234
abcd1235
.
.
.
abcd2000
Simply:
for (int a = 1234; a <= 2000; ++a)
{
std::cout << "abcd" << a;
}
In C,
for (int i=1234; i<= 2000;i++)
printf("abcd%d\n",i);

How can I shift signal in time 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 9 years ago.
Improve this question
I have a signal x(t). I must shift it a/2 and -a/2 and then take the sum of these two shifted signals. How can I write shift of x(t) in time in C++?
For time series data a shift in time is just an offset of n samples, so to sum data which is shifted by +/-a/2 samples:
for (i = a/2; i < N - a/2; ++i)
{
y[i] = x[i - a/2] + x[i + a/2];
}

How to round to the nearest fourth [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'm looking for a way to round a number to nearest number that can be divided by 4 without remainder
num = std::round(num / 4.0) * 4.0;
Here is some pseudo code. Probably not the most efficient way, but...
if num mod 4 == 0 then you are good
if num mod 4 == 1 then subtract 1
if num mod 4 == 2 then you decide (subtract/add 2)
if num mod 4 == 3 then add 1
Use the following MACRO:
#define ALIGN4(len) (((len) + 3) & ~3) // round up to 4 items