C++ gurus and learners, we program ourselves :P all work is CC licensed

Thursday, November 7

Smallest of integers alternate approach

19:48 By

//code by http://CppQA.blogspot.com
//we are community of developers focusing on help for new programmers
#include <iostream>
using namespace std;
#define CHAR_BIT 8

/*Function to find minimum of x and y*/
int min(int x, int y)
{
  return  x + ((y - x) & ((y - x) >>
            (sizeof(int) * CHAR_BIT - 1)));
}

/* Function to find minimum of 3 numbers x, y and z*/
int smallest(int x, int y, int z)
{
    return min(x, min(y, z));
}

int main()
{
   int x = 1, y = 100, z = 90;
   cout << "Minimum of 3 numbers is " << smallest(x, y, z) << endl<<"http://CppQA.blogspot.com"<<endl;
   return 0;
}

you can fork this code on ideone.com

hi my name is umar