Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
What does this function do ?
inline bool myFunc(int aVal) {return aVal & 1;}
Edit
I have edited the code to be runnable.
Keyword inline shall be written with lower case letters.
inline bool myFunc(int aVal) {return aVal & 1;}
The function returns true if the first bit of the value aVal is set to 1. Otherwise it returns false.
Using this function you can check for example whether aVal is odd or even number. :) If the function will return true then it means that the number is odd.
The operator & is bitwise AND operator.
Related
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 3 years ago.
Improve this question
Can any one explain this statement
int x=1,y=11,z;
z=x--&&y++¦¦--x;
I know the answer but not able to think that how it is coming
It pretty simple
x--
returns 1 (true). Therefore
y++
is evaluated. It returns 11
1 && 11
returns true. Since
true || X
is always true
--x
is not evaluated. The statement returns true (1).
This is called short-circuit evaluation. E.g.
a && b || c
means
if a is true
evaluate b
if a && b is false
evaluate c
You could rewrite it as
int x=1,y=11,z;
z=[&]() {
if (!(x--)) {
return false;
}
if (y++) {
return true;
}
if (x++) {
return true;
}
return false;
}();
It's important to notice that the order of evaluation is as described. That's one of the reasons you shouldn't overload operator&& or operator||. The described behavior only works if the operators are not overloaded.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
This is my code. It seems that f[i] returns 1 at any value of i.
int f(int x) { return 203; }
int main(){
cout<<f[0]<<' '<<f[21]<<' 'f[-1];//= 1 1 1
return 0;
}
Using the warning thrown by the compiler I understand that this is a pointer but it doesn't seem to behave like one.
f[-2](1) // = 203, good
f[32](1) // Process returned -1073741571 (0xC00000FD) execution time : 6.731 s
EDIT: I use the g++ compiler with the c++ 14 flag.
It is a GCC extension:
6.23 Arithmetic on void- and Function-Pointers
In GNU C, addition and subtraction operations are supported on pointers to void and on pointers to functions. This is done by treating the size of a void or of a function as 1.
A consequence of this is that sizeof is also allowed on void and on function types, and returns 1.
Resulting pointers to non-existent functions, if called, would most likely crash or produce weird results.
It seems that f[i] returns 1 at any value of i.
That's a well-known behaviour of cout. It prints all non-zero function pointers as 1, because there is no proper overload of operator<< for them, and operator<<(bool) gets chosen as a most suitable overload.
(f[i] is a function rather than a function pointer, but it decays to a pointer in this case.)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to use string::length() inside a function I wrote, named match(), but I'm getting garbage values.
Why does the marked line is outputting garbage values?
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
void match(string st1,string st2)
{
for(int i=0;i<st1.length();i++)
{
cout<<"Why this value is garbage "<<(i-st1.length()-1)<<"\t";
// this expression gives wrong values ^^^^^^^^^^^^^^^
}
}
int main()
{
string st1,st2;
cout<<"Enter the required string\n";
cin>>st1>>st2;
match(st1,st2);
return 0;
}
imagine a string "foo":
i-st1.length()-1 means:
when i is 0:
0 - 3 = -3
- 1 = -4
but st1.length() is a size_t, which is unsigned, so all terms in the expression are promoted to unsigned values.
(unsigned)0 - (unsigned)3 = 0xfffffffffffffffd
- 1 = 0xfffffffffffffffc
= 18446744073709551612
The problem is that i is an int value, while string::length will return you a size_t value. The first one is a signed value, while the second is unsigned. One way to prevent this is to cast your st1.length() as an int, so all the elements in your operation are signed values. You will then get the value you are looking for.
i-(int)st1.length()-1
This is not garbage, you are implicitly converting/promoting a signed type (i is signed int) to an 'unsigned' one (the return type of length() is size_t which is unsigned).
It happens implicitly because an unsigned type is more powerful than a signed one. This is a common source of bugs in the C/C++ world.
This is what you need to modify:
cout<<" **NOT** garbage "<<(i-(int)st1.length()-1)<<"\t"<<endl;
Happy programming!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a little problem.
Who knows how we can calculate the log
base n with Shift_L or Shift_R?
for example: for n=2 we had this solution:
int log(int n){
int res = 0;
while((n>>=1))
res++;
return res;
}
You don't seem to want the logarithm for a base b, but the largest integer n so that n <= log_b(x). If that's the case, the following function should serve your needs:
int intlog(double base, double x) {
return (int)(log(x) / log(base));
}
well this is rather a math problem instead of an actuall programming problem, if i understand your problem correctly:
log_2 (x) = log_a (x) / log_a (2) where a can be any base.
Therefore you could use the math.h's function log(double)
double res = log(x)/log(2);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int a = 101;
return 0;
}
Question : How do I know that the number (1) is repeated twice in the variable
If you look at the code, you will see that the number 101 is assigned to the variable a and that number has the digit 1 twice in its decimal representation. So direct inspection is the way to go. I wouldn't even write the code for such a trivial requirement.
Use modulus 10 and division 10 to find it. Rough idea is,
while( a > 0 )
{
if( a % 10 == 1 )count_one++;
a=a/10;
}