bit extraction

This is a discussion on bit extraction within the C Programming forums, part of the General Programming Boards category; Hey guys, I'm stuck at this program and cannot figure out the logic on how to solve it. I need ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    53

    bit extraction

    Hey guys,
    I'm stuck at this program and cannot figure out the logic on how to solve it.
    I need to extract 4 bits from an integer(and repeat it until i reach MSB) of a given integer.(my sizeof(int) = 4).and store them in separate character variables
    Example:
    int a = 011 1010 1101 1011 0011 1010(binary)
    counting from LSB
    t6=0011
    t5=1010
    t4=1101
    t3=1011
    t2=0011
    t1=1010

    How do i go about this problem?
    Thanks in advance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    b = a & 0xf; // extract 4 bits
    a = a >> 4; // shift

    Run in a loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extraction from long
    By P4R4N01D in forum C Programming
    Replies: 23
    Last Post: 06-01-2008, 03:25 PM
  2. CSS extraction
    By anandganapati in forum C Programming
    Replies: 1
    Last Post: 04-29-2008, 11:26 PM
  3. extraction of the C/C++ help
    By altu99999 in forum C Programming
    Replies: 2
    Last Post: 09-19-2005, 12:56 PM
  4. extraction of the C/C++ help
    By altu99999 in forum C Programming
    Replies: 2
    Last Post: 09-17-2005, 08:57 AM
  5. extraction
    By Max in forum C Programming
    Replies: 13
    Last Post: 07-15-2002, 07:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21