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

6 comments :

  1. but ideone is not completely free

    ReplyDelete
  2. idone and both are good but we can fork it using ideone

    ReplyDelete
  3. btw which one is better the old one or new from ideone people, i like both of these

    ReplyDelete
  4. i never saw that ideone.com has wrapped the lines

    ReplyDelete