Skip to content

Instantly share code, notes, and snippets.

@dfukunaga dfukunaga/radix.c
Created Nov 30, 2017

Embed
What would you like to do?
Select k-th (from LSB) radix of the number.
// Select k-th (from LSB) radix of the number.
// Author: dfukunaga (https://github.com/dfukunaga)
int radix1bit(int number, int k) {
return (number >> k) & 0b1;
}
int radix4bit(int number, int k) {
return (number >> (k << 2)) & 0xF;
}
int radix8bit(int number, int k) {
return (number >> (k << 3)) & 0xFF;
}
#include <stdio.h>
int main() {
int num1 = 0b1010;
for (int i = 0; i < 4; ++i) printf("%d\n", radix1bit(num1, i));
int num2 = 0x1234;
for (int i = 0; i < 4; ++i) printf("%d\n", radix4bit(num2, i));
int num3 = 0x1234ABCD;
for (int i = 0; i < 4; ++i) printf("%d\n", radix8bit(num3, i));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.