Easy Tutorial
❮ C Examples Five Fish Html C Function Strcpy ❯

C Exercise Example 54

C Language Classic 100 Examples

Title: Extract bits 4 to 7 from the right end of an integer a.

Program Analysis: Consider the following steps:

(1) First, shift a to the right by 4 bits.

(2) Set a number with the lowest 4 bits as 1 and the rest as 0, which can be achieved using ~(~0<<4).

(3) Perform the & operation on the above two.

Example

//  Created by www.tutorialpro.org on 15/11/9.
//  Copyright © 2015年 tutorialpro.org. All rights reserved.
//

#include <stdio.h>
int main()
{
    unsigned a,b,c,d;
    printf("Please enter an integer:\n");
    scanf("%o",&a);
    b=a>>4;
    c=~(~0<&lt;4);
    d=b&c;
    printf("%o\n%o\n",a,d);
    return 0;
}

The output of the above example is:

Please enter an integer:
36
36
1

C Language Classic 100 Examples

❮ C Examples Five Fish Html C Function Strcpy ❯