Advertisement
STANAANDREY

least sig nib

Nov 1st, 2022
1,050
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.56 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. void printBits(unsigned x) {
  4.   for (int i = sizeof(x) * 8 - 1; i >= 0; i--) {
  5.     printf("%d", 1 & (x >> i));
  6.     if (i % 4 == 0) {
  7.       putchar(' ');
  8.     }
  9.   }
  10.   puts("");
  11. }
  12.  
  13. int lsNib(unsigned a) {
  14.   const unsigned mask = (1 << 4) - 1;
  15.   const int nibNr = sizeof(a) * 2;
  16.   int pos = nibNr;
  17.   for (int i = 0; i < nibNr; i++) {
  18.     if ((a >> (i * 4)) & mask) {
  19.       pos = i;
  20.       break;
  21.     }
  22.   }
  23.   return pos;
  24. }
  25.  
  26. int main(void) {
  27.   unsigned a;
  28.   scanf("%u", &a);
  29.   printBits(a);
  30.   printf("%d\n", lsNib(a));
  31.   return 0;
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement