Bitwise

How to begin working for the answer inthis qns? It looks messy...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;

int defaultParam (int x = 30, int y = 20, int z = 10);
int staticVar();

int main ()
{
	int n = defaultParam (1,2,3);
	int m = defaultParam (4,5);
	
	cout << n << endl;
	cout << m << endl;
	cout << staticVar() << endl;
	
	cout << (n>>2) << endl;
	cout << ((n|m)<<2) << endl;
	
	return 0;
	
}

int defaultParam (int x, int y, int z)
{
	return x + y + z + staticVar();
}

int staticVar()
{

	static int x =2;
	x+=2;

	return x;

}
Last edited on
What?
I know this code includes bitwise but i was confuse whether to use x = 30 or 1, y = 20 or 2, z = 10 or 3?
Last edited on
You realise that line 4 and 23 need to be the same right?
Can elaborate further? Line 28 the answer is 4?
You realise that line 4 and 23 need to be the same right?
Wrong. Default parameters need to appear in either the definition or the declaration, but not both.

I still don't understand what OP is asking, though.
I know that Line 12:

1 + 2 + 3 + 4 = 10

I know that Line 16 = 2 (10/2 = 010 = 2)

How about Line 10 & Line 14?
Wrong. Default parameters need to appear in either the definition or the declaration, but not both.

Ok my mistake (although I did check my book to make sure you were correct... yes I doubted Helios!)

It does say though that:

" When creating a function that has default argument values, the default values must be specified only once, and this must happen the first time the function is declared within the file. " ... and then goes on to say the compiler will display an error if you try to do so in both prototype and definition.

Line 10: function is called with arguments, 4 and 5. Variable x is assigned 4 as it appears first, variable y is assigned 5 as it appears second, variable z is assigned 10 as no given value is specified.

x + y + z + staticVar()
4 + 5 + 10 + (2+2) = 23

Line 14: staticVar() contains a static variable which is initially assigned a value of 2. It is then gievn the addition of 2 and returned. By the time the program reached line 14, staticVar() has been called twice - line 14 being third. Therefore:

x = 2

x +=2 // first call
x = 4

x +=2 // second call
x = 6

x+=2 // third call
x = 8
Last edited on
Topic archived. No new replies allowed.