Skip to content

Commit

Permalink
Create getbits.c
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhangi47 authored Feb 4, 2024
1 parent 3e7cf2a commit fb25f8e
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions getbits.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <stdio.h>
/* getbits: get n bits from position p */
unsigned getbits(unsigned x, int p, int n) {
return (x >> (p + 1 - n)) & ~(~0 << n);
}

int main() {
unsigned x = 170; // Binary: 10101010
int p = 4;
int n = 3;

unsigned result = getbits(x, p, n);

printf("Result: %u\n", result); // Output: 5 (Binary: 101)
return 0;
}

0 comments on commit fb25f8e

Please sign in to comment.