number exersizes

I am a beginner in c++ and wanted to learn some tips and tricks with working on digits and numbers. for example to get the last digit of a number we do number%10 to remove the last number we do number/10 does anyone know of more useful tips and tricks like these. and also if anyone has any practice questions like "take a number and swap it" or "take a number and remove the first and last digit" thank you
Those are math hack, not specifically C++ (or programming in general) hacks.

These computers don't work in base 10, so they're kinda artificial in that environment.
there are literally millions of these things.

eg x >> 1 is the same as x/2 for integers but its infinitely faster because division circuit is huge, the shift circuit is tiny.

a ^=b; b^=a; a^=b; //inefficient and silly way to swap 2 numbers. it does more work than just assignments, but avoids using an extra register temporary in exchange for doing 3 times the work in a less readable way.

or logic is a form of addition. And is a form of multiply.

try to find something useful to work on. No offense, but all that 99% of these kinds of things are useful for is annoying interviewees and students by asking them to do things with artificial conditions or doing things that didnt need to be done. Your boss isnt going to ask you to swap numbers without using a temporary or to look for palindromes or peel the 5th digit out of an integer in base 10. If you just want to do them for fun, google interview questions for programmers and look at the programming contest type sites. They have this stuff in spades.


Last edited on
Topic archived. No new replies allowed.