Latest web development tutorials

C Exercise Example 54

100 cases of classic C language 100 cases of classic C language

Title: Take an integer from 4 to 7 from the right end of a beginning.

Program Analysis: consider this:

(1) the right to make a four.

(2) set up a low-4 are all 1, the rest are all zero numbers. Available ~ (~ 0 << 4)

(3) both of the above will be the & operator.

Source Code:

//  Created by www.w3big.com on 15/11/9.
//  Copyright © 2015年 本教程. All rights reserved.
//

#include <stdio.h>
int main()
{
    unsigned a,b,c,d;
    printf("请输入整数:\n");
    scanf("%o",&a);
    b=a>>4;
    c=~(~0<<4);
    d=b&c;
    printf("%o\n%o\n",a,d);
    return 0;
}

The above example output is:

请输入整数:
36
36
1

100 cases of classic C language 100 cases of classic C language