コード例 #1
0
ファイル: rightrot.c プロジェクト: junwi/tcpl
int main()
{
	int x = 12530;
	printIntBinary(x);
	printIntBinary(rightrot(x, 6));
	return 0;
}
コード例 #2
0
void processIntToken( char* inStr, struct argsInfo info ){
    long intToken;
    int i;
    
    ptrError = EINVAL;
    
    // Convert string to long
    intToken = strToLong(inStr, info.inputBase);
    
    /* Check for non-integer errors */
    if (errno == ptrError) {
        (void) fprintf(stderr, STR_INT_ENDPTR_ERR, inStr,\
                       info.inputBase);
        return;
    }
    
    /* Check for conversion errors */
    if (errno) {
        perror(inStr);
        return;
    }
    
    /* Print English mode */
    if (info.mode & E_FLAG) {
        
        /* Check for negative numbers */
        if (intToken < 0) {
            (void) printf(STR_MINUS);   // Print out minus sign
        }
        printEnglish(abs(intToken));
        (void) printf(STR_NEWLINE);
    }
    
    /* Print out the numbers in the given bases*/
    for (i = MIN_BASE; i <= MAX_BASE; i++) {
        
        // Get the actual base value based on the given flags
        int realBase = getRealBase(BASE_MASK(i) & info.outputBases);
        
        switch (realBase) {
            case 0:
                break;
                
            /* Print in binary representation*/
            case 2:
                printIntBinary(intToken);
                break;
            
            /* Print out prefix 0 and number in base 8 */
            case 8:
                
                /* Print out negative sign if negative number */
                if (intToken < 0) {
                    (void) printf(STR_NEG_PREFIX);
                }
                (void) printf(STR_OCT_PREFIX);
                printBase(abs(intToken), realBase);
                (void) printf(STR_NEWLINE);
                break;
            
            /* Print out prefix 0x and number in base 16 */
            case 16:
                
                /* Print out negative sign if negative number */
                if (intToken < 0) {
                    (void) printf(STR_NEG_PREFIX);
                }
                (void) printf(STR_HEX_PREFIX);
                printBase(abs(intToken), realBase);
                (void) printf(STR_NEWLINE);
                break;
            
            /* Print out number in given bases without prefixes */
            default:
                if (intToken < 0) {
                    (void) printf(STR_NEG_PREFIX);
                }
                printBase(abs(intToken), realBase);
                (void) printf(STR_NEWLINE);
                break;
        }
    }
}