Interesting short code snippets

A place to share and discuss technical news items, security breaches, new hardware/software etc.
Also any notes or queries not covered in the other forums.
Post Reply
User avatar
Manna5
Posts: 13
Joined: 2021-Aug-03, 7:54 am
Contact:

Interesting short code snippets

Post by Manna5 »

I have a cool piece of C code to share with, you. This function does find the greatest common dividor using the well-known variant of Euclidean algorithm with division. The interesting thing is how it can be written using C while statement, conditional and assignment operators.

Code: Select all

int gcd (int a, int b) {
        while ((a > b) ? (a %= b) : (b %= a));
        return a | b;
        }
You can also share your interesting snippets of code, including one-liners, in this thread.
Post Reply