-
Notifications
You must be signed in to change notification settings - Fork 0
/
ithbit.c
48 lines (36 loc) · 1018 Bytes
/
ithbit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* ithbit.c */
# include <math.h>
# include <stdlib.h>
# include <stdio.h>
# include <ctype.h>
# include <limits.h>
unsigned int divide ( unsigned int intNumberParam ) {
unsigned int intResult = 1;
if ( intNumberParam & 1 == 1) { //number is odd
return intResult;
}
else
{
//find max bit set and return 2^maxbit
while (intNumberParam >>= 1) {
intResult *= 2;
}
return (intResult * 2) - 1;
} //endif
} //end of divide function
////////////////////////////////////////////////
int main ( int argc, char * argv []) {
unsigned int intNumber;
char term;
//ask user for number
printf("Enter an integer and I'll display the numbers that will divide into it (in hex):\n");
//check it's an integer, quit if not
if(scanf("%d%c", &intNumber, &term) != 2 || term != '\n'){
printf("Unrecognised character - you must enter a valid integer.\n");
exit(0);
}
else {
//call divide with inputted number as param, and print result in hex
printf("Result in hex: %x\n",divide(intNumber));
}
}// end of main function