// Here's a little example to help explain the radix digit processing for asn 6 // I put it on the web page as "masking.c" #include main() { int n, radix, digit, mask; // In base ten we would have to shift digits by dividing // and then extract a digit using the modulus operation. // For example to get the 4th radix 10 digit from a number n n = 123456789; radix = 10; digit = (n / (radix*radix*radix)) % radix; printf("n %d the 4th base 10 digit is %d\n", n, digit); // To get the series of digits in order we can loop ... for( ; n; n /= radix) printf("digit is %d\n", n % radix); // The same logic works in other bases so for base 8 octal n = 01234567; radix = 8; printf("octal n is 0%o\n", n); for( ; n; n /= radix) printf("octal digit is %d\n", n % radix); // Or for base 16 hexadecimal n = 0x1234ABCD; radix = 16; printf("hex n is 0x%x\n", n); for( ; n; n /= radix) printf("hex digit is %d = 0x%x\n", n % radix, n % radix); // Or for base 2 binary n = 0x1234ABCD; printf("hex n is 0x%x\n", n); radix = 2; for( ; n; n /= radix) printf("binary digit is %d\n", n % radix); // However, division is very slow so we can do things much // more quickly by working with bases that are powers of 2 // so we can do digit extraction by bit shifting and masking. // The previous example in base 2 is also ... n = 0x1234ABCD; printf("hex n is 0x%x\n", n); for( ; n; n >>= 1) printf("binary digit is %d\n", n & 1); // Redone in radix 16 to give hex digits by 4 bit shifts and 0xF mask n = 0x1234ABCD; printf("hex n is 0x%x\n", n); for( ; n; n >>= 4) // shift by 4 bits and mask with 0xF which is 15 printf("hex digit is %d = 0x%x\n", n & 0xF, n & 0xF); // Notice that for any power of 2 radix, like 16 above, the mask // is just radix-1 and the shift is the number of trailing 0's // in the binary representation. That should let you understand // the defines that are in the asn6 template ... #define BITS 8 /* specify the radix by number of bits it uses */ #define RADIX (1<