Haha, OK i will give you an hint but not the answer. So relational operator should be used. But they dint say that cant use arithmetically operator did they???? Is that more than enough to give you a hint.
int max1(int a, int b)
int chxvar = 1 << (8 * sizeof(int) - 1); //This initialises to a integer
if( (a - b) & chxvar) //of the value(100000000..)
From the code I get the hint you are checking the sign bit here, this solution would not always work. For example consider the situation where a = MAX_INT_VALUE; and b=MIN_INT_VALUE; In such a scenario (a-b) would be equal to "-1" which is less than zero, due to overflow.
MAX_INT_VALUE and MIN_INT_VALUE mentioned only for illustration,
For my compiler (GCC 4.1.2) they are
I don't really see a great importance of this code here
int chxvar = 1 << (8 * sizeof(int) - 1);
If an int variable is set to "1", the very last bit is set to 1 and rest all bits are 0, 1 << (8 * sizeof(int) - 1) moves that 1 bit to the very start of memory location of that int variable, so it can be directly anded with (a-b) , to check if the sign bit is set or not.
Theres another draw back I just spotted here, if a=b, a-b=0 therefore the sign bit will not be set and the function will return saying a is greater than b.
Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...