Thread: Printing certain bits

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    61

    Printing certain bits

    Hi There,

    I have a hexadecimal number anything between 0 and ffff, what is the simplist way of printing certain bits. Reason im asking is the hexideciamal is broken down into its individual bits and each bit has its own meaning, like bit 1 if high means power present low means no power etc.

    What i want to end up with is

    printf("Power Present %x, (First bit of the number));
    printf("Switched On %x,(Second bit of the number));

    etc lol

    thanks in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You aren't really clear as to what it is you want to print. I assume you want to print something different if a specific bit is set.
    Code:
    if( ISSET( value, bit ) )
        printf( "Bit %d is set\n.", bit );
    If that's the case, take a look at this: FAQ > Bit shifting and bitwise operations - Cprogramming.com


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by r_james14 View Post
    Hi There,

    I have a hexadecimal number anything between 0 and ffff, what is the simplist way of printing certain bits. Reason im asking is the hexideciamal is broken down into its individual bits and each bit has its own meaning, like bit 1 if high means power present low means no power etc.

    What i want to end up with is

    printf("Power Present %x, (First bit of the number));
    printf("Switched On %x,(Second bit of the number));

    etc lol

    thanks in advance
    Code:
    printf("Power is %s\n", (val & 1) ? "on" : "off");
    printf("Switch is %s\n", (val & 2) ? "on" : "off");
    printf("Fan is %s\n", (val & 4) ? "on" : "off");
    //etc.
    The trick is ... condition ? true : false ... acts like an inline if() statement.

    The values of each bit are powers of 2 ... 1, 2, 4, 8, 16, 32, 64, 128, etc.

    It's equivalent to...
    Code:
    printf( "Power  is ");
    if ((val & 1) > 0 )
      printf("on\n");
    else
     printf("off\n");
    Just in more compact syntax.
    Last edited by CommonTater; 01-03-2012 at 07:06 AM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Although I know my powers of two up to 262144, I prefer to use hexadecimal to represent bit masks in something like the above:
    Code:
    printf("Engine is %s\n", (val & 0x08) ? "on" : "off");
    printf("Navigation system is %s\n", (val & 0x10) ? "on" : "off");
    printf("Thrusters are %s\n", (val & 0x20) ? "on" : "off");
    A nicer way that enables you to somewhat number the bits is to use a rightshift:
    Code:
    printf("Communication systems are %s\n", ((val >> 6) & 1) ? "on" : "off");
    printf("Weapons are %s\n", ((val >> 7) & 1) ? "on" : "off");
    printf("World domination is %s\n", ((val >> 8) & 1) ? "on" : "off");
    (Note the 6, 7, and 8, the values from 0 to 15 correspond to the various bits starting from 0=rightmost)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing with c - specify certain bits inside a byte
    By bosque in forum C Programming
    Replies: 7
    Last Post: 02-21-2011, 04:09 PM
  2. Printing non-printing characters in ^ and M- notation
    By sbeard22 in forum C Programming
    Replies: 6
    Last Post: 10-03-2008, 11:12 PM
  3. Extracting certain bits from sequence of bits
    By lucaspewkas in forum C Programming
    Replies: 5
    Last Post: 10-06-2007, 12:22 AM
  4. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM
  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