Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
C++
#include <stdio.h>
void main()
{
    long broj = 1123583145;
    long cif, rez = 0;
	
    while( broj )
    {
        cif = broj % 10;
	rez = ( cif & 1 ) ? rez * 10 + cif : rez;
	broj = broj / 10;			
    };
    printf( "%li", rez );
}


What I have tried:

Can someone explain me how did I get 5135311??
Posted
Updated 29-Nov-19 8:00am
v3
Comments
[no name] 29-Nov-19 11:55am    
First of all: All your c# tags you can remove. C# does not accept rez = ( cif & 1 ) ? rez * 10 + cif : rez; especally the part ( cif & 1 )
Second: It looks like you don't know what the operator "%" means? In case this is right google for "modulo". And more I'm not sure wheter you understand what " ( cif & 1 ) ? ..." means. For this google for "ternary operator"

Because that is what you told the computer to do.

Because broj % 10 gives you the least significant digit.
It discards all even numbers: cif & 1 sees to that.
Then it moves each odd digit "up" one digit position.
Which means that the least significant odd digit of the original number ends up as the most significant of the result, and so on.

Break out the debugger and run your code through it step by step - you'll see what I mean.

Quote:
I still do not understand why it removes all even numbers. % 10 divide 1123583145 with 10 and it stores remainder which is 5 in this case. 5 & 1 means true and that is first number, okay. When we repeat the process what happens to 4?


Basically, you're confused by the conditional operator.
When you write
C++
a = B ? c : d;
you are really writing this in a short form:
C++
if (B)
   {
   a = c;
   }
else
   {
   a = d;
   }
So your code looks like this:
C++
while( broj )
    {
    cif = broj % 10;
    if (cif & 1)
       {
       rez = rez * 10 + cif;
       }
    else
       {
       rez = rez;
       }
    broj = broj / 10;
    };
So, you can discard the else part entirely - it does nothing at all - and simplify your code to this:
C++
while( broj )
    {
    cif = broj % 10;
    if (cif & 1)
       {
       rez = rez * 10 + cif;
       }
    broj = broj / 10;
    };
Which is a lot more obvious as to what is happening!
Since cif & 1 is only true if cif is an odd number (because that's how the & operator works) you only do anything with odd valued digits, you ignore even ones.
 
Share this answer
 
v2
Comments
Levin101q 29-Nov-19 12:05pm    
I still do not understand why it removes all even numbers. % 10 divide 1123583145 with 10 and it stores remainder which is 5 in this case. 5 & 1 means true and that is first number, okay. When we repeat the process what happens to 4?
OriginalGriff 29-Nov-19 12:41pm    
Answer updated
Simple: you're working through the digits of broj from right to left, and adding each odd digit to rez.

The odd digits of broj are 5, 1, 3, 5, 3, 1, and 1.

Therefore, your result is 5135311.

If you still don't understand, use the debugger to step through your code line by line and examine the variables at each step.

Edit:
Computers store numbers using binary[^].

For example:
5 (base 10) === 0101 (base 2)
4 (base 10) === 0100 (base 2)

When applied to numbers, the & operator performs a bitwise-AND[^] on the numbers. Each pair of binary digits in the two operands will produce a binary digit in the result. The result digit will be 1 if both inputs are 1; otherwise, the result digit will be 0.

So:
5 & 1 (base 10) === 0101 & 0001 (base 2) === 0001 (base 2) === 1 (base 10)
4 & 1 (base 10) === 0100 & 0000 (base 2) === 0000 (base 2) === 0 (base 10)

In binary, all odd numbers have the right-most digit set to 1, and all even numbers have the right-most digit set to 0.

So (odd number) & 1 === 1, and (even number & 1) === 0.

C and C++ treat non-zero numbers as TRUE, and zero as FALSE. Therefore, in your ternary expression, you evaluate the "true" part if cif is odd, and the "false" part if cif is even.
 
Share this answer
 
v4
Comments
Levin101q 29-Nov-19 12:05pm    
I still do not understand why it removes all even numbers. % 10 divide 1123583145 with 10 and it stores remainder which is 5 in this case. 5 & 1 means true and that is first number, okay. When we repeat the process what happens to 4?
OriginalGriff 29-Nov-19 12:17pm    
Answer updated
Richard Deeming 29-Nov-19 12:21pm    
Did you mean to update my answer? :)
OriginalGriff 29-Nov-19 12:41pm    
No, I missed ... :doh:
Hmm...
I tried this out as JavaScript so I could open the browser Dev tools (F12) and run it in the console:

JavaScript
var rez = 0; var broj = 1123583145;
while (broj > 0){ 
    cif = broj %10; 
    rez = (cif & 1) ? Math.trunc(rez * 10 + cif) : rez;  // uses ternary operator
    console.log("rez : " + rez);
    broj = broj /10; 
    console.log("broj : "+ broj)
} 
console.log(rez);


If you open the Browser console window (F12 in most browsers and paste and run that code you'll see that it shows you how you get the value.

Basically I added output each time through the loop so you can see the values of broj and rez each time.

Also, do you understand the line I marked // uses ternary operator?
JavaScript
rez = (cif & 1) ? Math.trunc(rez * 10 + cif) : rez;


That line does a bitwise operation on the value of cif and 1.
The first time through cif = 5; (1123583145 % 5) // 1123583145 mod 5
since cif = 5 then binary value is 0101
The & is bitwise and so the (cif & 1) will
0101
0001
----
0001

The value of cif & 1 = 1
Since 1 evaluates to true then the left side of the ternary operator is completed:
Math.trunc(rez * 10 + cif) // trunc function is to insure it is an integer value
rez * 10 = 0 // since rez starts out at 0
0 + 5 (value of cif) means that rez gets set to 5


Below are the first N times through the loop, where you can see what is happening.
You can see it is taking each digit of the initial value and building it up.
I leave the rest to you to examine.

rez : 5 
broj : 112358314.5 
rez : 5 
broj : 11235831.45 
rez : 51 
broj : 1123583.145
rez : 513 
broj : 112358.31450000001
rez : 513 
broj : 11235.831450000001 
rez : 5135 
broj : 1123.583145 
rez : 51353 
broj : 112.3583145
rez : 51353 
broj : 11.235831450000001
rez : 513531 
broj : 1.123583145
rez : 5135311 
broj : 0.1123583145 
broj : 0.01123583145 
broj : 0.0011235831450000001
 
Share this answer
 
Quote:
Can someone explain me how did I get 5135311??

Why don't you ask your computer to show you with the help of debugger ?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
[no name] 29-Nov-19 13:54pm    
Sorry, the one with the one was me. I think your "solution" is completely misplaced in this case.
Patrice T 29-Nov-19 15:53pm    
Hi,
First of all, thanks for the comment. At least you have the courage of your opinions, many downvoters stay anonymous.

My experience is that the debugger is always useful to understand why a program do not behave as expected. So I think the sooner one learn to use it, the better.
I think my solution is fit as there was already 3 other solutions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900