I don't know what does this mean (x^y) ? in c++ - c++

what is this function used for ? and it's not for the power function. :
#include<iostream>
using namespace std;
int main(){
int x,y;
cout<<(x^y)<<endl;/* this is the unkown (X^Y)*/
return 0;
}

The ^ operator is the bitwise XOR. Take for example 6 and 12
6 in binary is: 110
12 in binary is: 1100
The xor follows this rule: "The first or the second but not both". What does it mean? Its truth table should make it clear:
A B A^B
0 0 0
0 1 1
1 0 1
1 1 0
You can see that the only 1-bits are those where or A or B (but not both) are set.
Back to the first example:
A 1100 => 12
B 0110 => 6
A^B 1010 => 10

It's XOR. If you want more information about it see here https://en.wikipedia.org/wiki/Exclusive_or

Power function in c++ is
#include <math.h>
#include <iostream>
int main()
{
int x, y;
std::cout << "Give numbers " << std::endl;
std::cout << "x = ";
std::cin >> x;
std::cout << "y = ";
std::cin >> y;
std::cout << "Result = " << pow(x, y) << std::endl;
return 0;
}
Your version is XOR (logical operation) which is used to for example embedded systems etc.

Related

C++ giving a negative number when I square

So I have some code that is supposed to be giving me the points on a parabola but the problem is that when I square the number when it is a negative it gives me a negative back which wont work.
#include "TileClass.h"
//#include <cmath> included in other header
// Original equation y=-x^2+4
void Tile::Loc() {
for (int a = -2; a < 3; a = a + 1) {
cout << "--- " << a << endl;
cout << "Pw" << (a << 2) << endl;
cout << ((a << 2) + 4) << endl;
}
}
output
--- -2
Pw-8
-4
--- -1
Pw-4
0
--- 0
Pw0
4
--- 1
Pw4
8
--- 2
Pw8
12
The C++ operator << is not the square operator. It is the bitshift operator.
Try
-(a*a)+4
instead
The express a<<2 will shift each bit of a to left by 2, which means a*4
And if you want to get the square of a, you had better use a*a.
it doesn't look like you're squaring. You're left shifting the binary value which is essentially the same as multiplying by 2 per left shift. So when a = -2, doing (a << 2) should result in -8 being displayed. What you are looking for is the pow() function found in the cmath library which should be used like this pow(a, 2);

Computing fibonacci like function in sublinear time

So, here is the problem:
Given a number n I need to calculate the amount of calls it would need to calculate the fibonacci of that number recursively and output only the last digit in a given base as a decimal. The input comes as 2 numbers being the first the number n and the second the base that the output should be in. For the output should be the case number, the first input, the second input and the result of the calculation. The program should exit when the first entry is equal to the second entry that equals 0. For example:
Input:
0 100
1 100
2 100
3 100
10 10
3467 9350
0 0
Output:
Case 1: 0 100 1
Case 2: 1 100 1
Case 3: 2 100 3
Case 4: 3 100 5
Case 5: 10 10 7
Case 6: 3467 9350 7631
I have arrived on the following formula when trying to solve this. Being c(n) the last digit of the number of calls it would take in a base b, we have that:
c(n) = (c(n-1) + c(n-2) + 1) mod b
The problem is that n can be any value from 0 to 2^63 - 1 so I really need the code to be efficient. I have tried doing in a iterative way or using dynamic programming but, although it give me the right output, it doesn't give me in a short enough time. Here is my code:
Iterative
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<unsigned long long int> v;
unsigned long long int x,y,co=0;
cin >> x >> y;
while(x||y){
co++;
v.push_back(1);
v.push_back(1);
for(int i=2;i<=x;i++) v.push_back((v[i-1]+v[i-2]+1)%y);
cout << "Case " << co << ": " << x << " " << y << " " << v[x] << endl;
cin >> x >> y;
v.clear();
}
return 0;
}
Dynamic programming
#include <iostream>
#include <vector>
using namespace std;
vector<unsigned long long int> v;
unsigned long long c(int x, int y){
if(v.size()-1 < x) v.push_back((c(x-1,y) + c(x-2,y) + )%y);
return v[x];
}
int main(){
int x,y,co=0;
cin >> x >> y;
while(x||y){
co++;
v.push_back(1);
v.push_back(1);
cout << "Case " << co << ": " << x << " " << y << " " << c(x,y) << endl;
cin >> x >> y;
v.clear();
}
return 0;
}
x and y are respectively n and b, v holds the values for c(n)
Every c in the sequence is less than b. So there are b possibilities for the value of a c. So a pair of consecutive elements [ck, ck+1] can have b2 possible values. So if you start at the beginning and calculate c1, c2, c3... you will have to calculate at most b2 of them before the sequence begins to repeat; you will come to a [ck, ck+1] that is equal to an earlier [cj, cj+1].
Then you know the length of the cycle, call it S, and you know that cn = c((n-j)mod S)+j for all n > j. That should cut your work down quite a lot.

for loop multiple variable, second variable not updating

I am trying to write two loops in one for loop so I looked up the syntax for multiple variables in the for loop
the problem is the second variable l isn't updating I don't know why
#include<iostream>
using namespace std;
int main ()
{
float vsum=0, lsum=0;
double nsum=0, msum=0;
float v=1, l=100000000;
for (v, l ; v<= 100000000, l >= 1 ; v++, l--)
{
vsum= vsum + 1/v;
nsum= nsum + 1/v;
lsum= lsum + 1/l;
msum= msum+ 1/l;
}
cout << " The float sum of all numbers 1 through 1/100000000 is " << vsum << endl;
cout << " The double sum of all numbers 1 through 1/100000000 is " << nsum << endl;
cout << "The float sum of all numbers 1/100000000 through 1/1 is " << lsum << endl;
cout << "The double sum of all numbers 1/100000000 through 1/1 is " << msum << endl;
cin >> vsum;
}
I guess your question is that after
float f = 100000000;
why does --f; leave f unchanged?
The answer is due to the granularity of float. The float does not have enough accuracy to store every possible integer. Clearly a 32-bit float cannot store as many integer values as a 32-bit int, for example.
The further away from 0 you get, the larger the gap gets between successive possible values of a float. On your system 100000000 - 1 is still larger than the next possible value of float below 100000000.
The rules of C++ are that when the result of the calculation is not representable exactly by a float, then it's implementation-defined whether the next-lowest value or the next-highest value is used. (So your compiler should actually document what happens here). In this case your system is using the next-highest value.
To get your intended results, make v and l be integral types, and do a float conversion in the actual calculation, e.g.
vsum += 1.f/v;
nsum += 1.0/v;
As dasblinkenlight mentions, you are only checking the second condition, but the second variable is updating just fine. Here is an abridged example that proves this.
#include<iostream>
using namespace std;
int main ()
{
float vsum=0, lsum=0;
double nsum=0, msum=0;
float v=1, l=10;
for (v, l ; v<= 10, l >= 1 ; v++, l--)
{
cout << v << " " << l << endl;
}
}
Output:
1 10
2 9
3 8
4 7
5 6
6 5
7 4
8 3
9 2
10 1

Thinking in C++ shift operators

I'm reading through a book on C++ standards: "Thinking in C++" by Bruce Eckel.
A lot of the C++ features are explained really well in this book but I have come to a brick wall on something and whether it may or may not help me when I wish to program a game for example, it's irking me as to how it works and I really cannot get it from the explanation given.
I was wondering if anybody here could help me in explaining how this example program actually works:
printBinary.h -
void printBinary(const unsigned char val);
printBinary.cpp -
#include <iostream>
void printBinary(const unsigned char val) {
for (int i = 7; i >= 0; i--) {
if (val & ( 1 << i))
std::cout << "1";
else
std::cout << "0";
}
}
Bitwise.cpp -
#include "printBinary.h"
#include <iostream>
using namespace std;
#define PR(STR, EXPR) \
cout << STR; printBinary(EXPR); cout << endl;
int main() {
unsigned int getval;
unsigned char a, b;
cout << "Enter a number between 0 and 255: ";
cin >> getval; a = getval;
PR ("a in binary: ", a);
cin >> getval; b = getval;
PR ("b in binary: ", b);
PR("a | b = ", a | b);
This program is supposed to explain to me how the shift bitwise operator (<<) and (>>) work but I simply don't get it, I mean sure I know how it works using cin and cout but am I stupid for not understanding this?
this piece in particular confuses me more so than the rest:
if (val & ( 1 << i))
Thanks for any help
if (val & ( 1 << i))
Consider the following binary number (128):
10000000
& is bitwise "AND" - 0 & 0 = 0, 0 & 1 = 1 & 0 = 0, 1 & 1 = 1.
<< is bitwise shift operator; it shifts the binary representation of the shifted number to left.
00000001 << 1 = 00000010; 00000001 << 2 = 00000100.
Write it down on a piece of paper in all iterations and see what comes out.
1 << i
takes the int-representation of 1 and shifts it i bits to the left.
val & x
is a bit-wise AND between val and x (where x is 1 << i in this example).
if(x)
tests if x converted to bool is true. Any non-zero value of an integral type converted to bool is true.
<< has two different meanings in the code you shown.
if (val & (1 << i))
<< is used to bitshift, so the value 1 will be shifted left by i bits
cout << ....
The stream class overloads the operator <<, so here it has a different meaning than before.
In this case, << is a function that outputs the contents on its right to cout
if (val & ( 1 << i))
This checks if the bit in i-th position is set. (1 << i) is something like 000001000 for i = 3. Now if the & operation returns non-zero, that means val had the corresponding bit set.

What does an extra 0 in front of an int value mean?

Inspiring from a obfuscated piece of code, I have a small question regarding to assign value to an integer:
#include <iostream>
#include <cstdio>
int main() {
int i = 0101;
std::cout << i << "\n";
}
And the output was 65, and I have no idea where 65 came from? Any idea?
It specifies an octal (base-8) number: 0101 == 1 * (8 * 8) + 1 == 65.
Lambert already explained that. So let me tell you what else you can do.
You can write hexadecimal integer:
int main() {
int i = 0x101; //0x specifies this (i.e 101) is hexadecimal integer
std::cout << i << "\n"; //prints 257 (1 * 16 * 16 + 1)
}
Output:
257