Skip to content

Commit

Permalink
Create ExtractBits.c
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhangi47 authored Feb 5, 2024
1 parent fb25f8e commit ce7fd5c
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions ExtractBits.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdio.h>
#include <stdint.h>
/* Extracting bits from a byte */
uint32_t extractBits(uint8_t number, int startBit, int endBit) {
startBit = (startBit < 0) ? 0 : (startBit > 8) ? 8 : startBit;
endBit = (endBit < 0) ? 0 : (endBit > 8) ? 8 : endBit;


if (startBit > endBit) {
int temp = startBit;
startBit = endBit;
endBit = temp;
}


uint8_t bitmask = ((1U << (endBit - startBit + 1)) - 1) << startBit;


uint8_t extractedBits = (number & bitmask) >> startBit;

return extractedBits;
}

int main() {
uint8_t number = 0b10010101;

int startBit = 7;
int endBit = 7;

uint8_t result = extractBits(number, startBit, endBit);

printf("Original number: 0b%b\n", number);
printf("Extracted bits : 0b%b\n", result);

return 0;
}

0 comments on commit ce7fd5c

Please sign in to comment.