Question:

In an 8 bit word how can i extract 2 bits at a time?

by  |  earlier

0 LIKES UnLike

eg. 10011011, i need 10, then 01 then 10 and so on...

but if the registers hold 8 bit of data, what will happen to the remaining bits?

can some one give me a code?

 Tags:

   Report

3 ANSWERS


  1. EDIT:

    Er, you still didn't mention which CPU, but I can see by your other question that you're using the 16c54 and 8051 microcontrollers.  I haven't programmed either of those chips before, but I can handle assembly language programming.

    In your example, the x's (lower 3 bits of each nibble) are supposed to be zero, right?

    I downloaded the data sheet for the 16c54 and I'm assuming that you want the code for that microcontroller.

    Here's my algorithm using RRF, BTFSC and BSF.

    (Warning: The syntax may not be correct.)

    1) Use RRF (if necessary) to rotate the test byte's bit pair into the 2 rightmost bit positions (bit positions 1 and 0).

    2) Call the procedure below.

    PROCEDURE:

    (use some instruction to zero out a spare file register)

    BTFSC testbyte,1 ;test bit 1

    BSF file_register,7 ;not executed if bit 1 not set

    BTFSC testbyte,0  ;test bit 0

    BSF file_register,3 ;not executed if bit 0 not set

    There.  Now the file register is setup properly, and the 3 lower bits of each nibble are zero.

    ----------

    Which CPU are you programming?


  2. #include <stdio.h>

    #define BIT_PATT (unsigned char) 0x9b

    main()

    {

        unsigned char bp, two_bits[4];

        int i;

        bp = BIT_PATT;

        for (i = 0; i < 4; i++)

        {

            two_bits[3 - i] = bp & 3;

            bp >>= 2;

        }

        for (i = 0; i < 4; i++) switch(two_bits[i])

        {

        case 0: printf("00\n");

            break;

        case 1: printf("01\n");

            break;

        case 2: printf("10\n");

            break;

        case 3: printf("11\n");

            break;

        }

    }

  3. Try creating a character array that will hold the original data. Then pull them off by extracting them using their index number. Since they will be characters they will follow the 8bit ascii standards so '1' will actually equal "00011000" (decimal 48). I hope this gives you some ideas.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.