Thread: How to get the 20 least significant bits?

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    23

    How to get the 20 least significant bits?

    I have some strings that come in over a REST interface that represent some network coordinates. The string looks something like:

    02:00:00:00:00:07

    Bits 5 - 0 represent tier 1
    bits 13 - 6 represent tier 2
    bits 19 - 14 represent tier 3.

    How would I parse these out in C?

    I've tried looking online, but I found it quite confusing.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,637
    I don't understand your string format, but if you have an unsigned integer and you want those bits as separate values:
    Code:
    #include <stdio.h>
     
    int main() {
        unsigned n = 0xabcde; // 101010 11110011 011110
                              // 19-14  13-6     5-0
     
        unsigned a = (n >> 14) & 0x3f;
        unsigned b = (n >>  6) & 0xff;
        unsigned c =  n        & 0x3f;
     
        printf("%02x (%3u)\n", a, a); // 2a (   42)
        printf("%02x (%3u)\n", b, b); // f3 (  243)
        printf("%02x (%3u)\n", c, c); // 1e (   30)
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Assuming this "string" is a MAC address (6 bytes):
    Code:
      union {
        unsigned int dw;
        unsigned char b[8];
      } u = { 0 };
    
      // str points to string...
      if ( sscanf( str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
        &u.b[5], &u.b[4], &u.b[3], &u.b[2], &u.b[1], &u.b[0] ) != 6 )
      { ... error ... }
      ...
    Now you can use u.dw as john.c showd above.

  4. #4
    Registered User
    Join Date
    Oct 2017
    Posts
    23
    Quote Originally Posted by flp1969 View Post
    Assuming this "string" is a MAC address (6 bytes):
    Code:
      union {
        unsigned int dw;
        unsigned char b[8];
      } u = { 0 };
    
      // str points to string...
      if ( sscanf( str, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
        &u.b[5], &u.b[4], &u.b[3], &u.b[2], &u.b[1], &u.b[0] ) != 6 )
      { ... error ... }
      ...
    Now you can use u.dw as john.c showd above.
    Yes, that's exactly what it is, a MAC address. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing particular bits and detecting toggled bits
    By Nordomus in forum C++ Programming
    Replies: 11
    Last Post: 04-13-2018, 12:07 PM
  2. How can I extract the most significant 4 bits?
    By mr_coffee in forum C Programming
    Replies: 6
    Last Post: 04-03-2009, 01:40 PM
  3. Significant Digits
    By strokebow in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2008, 04:30 AM
  4. significant numbers
    By Chaplin27 in forum C++ Programming
    Replies: 9
    Last Post: 04-04-2005, 05:01 PM
  5. copy some bits into a 8 bits binary number
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 05-29-2002, 10:54 AM

Tags for this Thread