1,142 views
2 2 votes

long fun( char * s){
    long r = 0;
    for( ; *s ; r = (r << 1) | (*s++ - '0'));
    return r;
}

What does the fun("000001010") returns?

2 Answers

Best answer
3 3 votes

(*s++ - '0') this will subtract ascii value of stored character with ascii value of 0 and will give numeric (decimal ) value of stored character during each iteration,

example: ascii value of 0 and 1 is 48 and 49 , hence during iteration 1:

(*s++ - '0')=s[0]-'0'='0'-'0'=48-48=0(decimal value)

now evaluation of ur program is as given below : fun("000001010")

iteration r<<1 (*s++-'0')=value stored at s[i] r = r<<1|(*s++ - '0')
1 0 0 0
2 0 0 0
3 0 0 0
4 0 0 0
5 0 0 0
6 0 1 1
7 2 0 2
8 4 1 5
9 10 0 10
10   \n(ascii value 0) loop fail

therefore 10 will be return at the end

• selected by
1 1 vote
r   s

0  0                                                                                                                                                                            

0  0                                                                                                                                                                            

0  0                                                                                                                                                                            

0  0                                                                                                                                                                            

0  0                                                                                                                                                                            

0  1                                                                                                                                                                            

1  0                                                                                                                                                                            

2  1                                                                                                                                                                            

5  0     

at last r = r*2 = 10

 

return value will be 10
Position:
Show:

Related questions

1 1 vote
1 1 answer
241
241 views
khanshahima4 asked Sep 24
241 views
1. If precedence tells me which operator has priority, and associativity tells me how to handle operators at the same level, then why do I need a separate concept called ...
14 14 votes
1 1 answer
1.3k
1.3k views
GO Classes asked Jul 2
1,314 views
Assume:$\texttt{short = 2 bytes}$, $\texttt{int = 4 bytes}$, $\texttt{char = 1 byte}$$\texttt{int}$ needs $4$-byte alignment and the final structure size is rounded to a ...
17 17 votes
2 2 answers
720
720 views
GO Classes asked Jul 2
720 views
Assume:struct Student { double gpa; }; struct Student alice; struct Student *sptr = &alice;Which of the following correctly assigns $\texttt{4.0}$ to $\texttt{alice.gpa}$...
10 10 votes
1 1 answer
544
544 views
GO Classes asked Jul 2
544 views
Consider the following code idea from the source:typedef struct { double x, y; } Point; void reset(Point p) { p.x = p.y = 0; } int main() { Point a = {12.0, 42.0}; Point ...