Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int replicateSign(int x) {
- int sign=x>>31;
- // replicate the bit into the entire int
- int tmp = sign|(sign<<1); // 2lsb
- tmp = tmp|(tmp<<2); // 4lsb
- tmp = tmp|(tmp<<4); // 8lsb
- tmp = tmp|(tmp<<8); // 16lsb
- tmp = tmp|(tmp<<16); // 32lsb
- return tmp;
- }
- int abs1(int x) {
- int signMask=replicateSign(x);
- int negX=-x;
- // apply masks; if sign bit is 1 (meaning negative), we want -x,
- // and if it is 0 we want x
- return (signMask&negX) | ((~signMask)&x);
- }
- int max(int a,int b) {
- return (a+b+abs1(a-b))/2;
- }
- int main(int argc, char** argv) {
- if(argc<3) {
- fprintf(stderr,"usage: %s a b\n",argv[0]);
- return 1;
- }
- printf("%d\n", max(atoi(argv[1]), atoi(argv[2])));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement