+ Post New Thread
Results 1 to 8 of 8
  1. #1
    Newbie level 6
    Points: 283, Level: 3

    Join Date
    Sep 2014
    Posts
    13
    Helped
    0 / 0
    Points
    283
    Level
    3

    Turning two bits into one two-bit binary number

    Hello everyone. I need help on a homework assignment:

    Write a C program that treats A1A0, A3A2, and A5A4 as three 2-bit unsigned binary
    number. The program should output the sum of those three numbers onto B.

    I need help with the 'treat A1A0... 2-bit unsigned binary number.' I figure I'd have to use the GetBit function but i dont know how to get two bits and make them a one two-bit number.
    Thank you in advance.

    •   AltAdvertisement

        
       

  2. #2
    Advanced Member level 5
    Points: 12,735, Level: 27
    Achievements:
    7 years registered

    Join Date
    Nov 2008
    Location
    Norwich, UK
    Posts
    2,123
    Helped
    598 / 598
    Points
    12,735
    Level
    27

    Re: Turning two bits into one two-bit binary number

    I assume you are reading the bits of PORTA
    you can extract the bits so
    Code:
    int A10=PORTA & 3;
    int A32 = (PORTA >> 2) & 3
    etc
    is that what you want to do?



  3. #3
    Newbie level 6
    Points: 283, Level: 3

    Join Date
    Sep 2014
    Posts
    13
    Helped
    0 / 0
    Points
    283
    Level
    3

    Re: Turning two bits into one two-bit binary number

    I dont know what PORTA is, but I'm using RIMS software, which is a microcontroller simulator. This is what it looks like.




    •   AltAdvertisement

        
       

  4. #4
    Advanced Member level 5
    Points: 12,735, Level: 27
    Achievements:
    7 years registered

    Join Date
    Nov 2008
    Location
    Norwich, UK
    Posts
    2,123
    Helped
    598 / 598
    Points
    12,735
    Level
    27

    Re: Turning two bits into one two-bit binary number

    if A1 and A0 are one bit variables to create a two bit variable you shift A1 one biit left and add A0 to it
    Code:
    A1A0=(A1<<1) + A0;



  5. #5
    Newbie level 1
    Points: 14, Level: 1

    Join Date
    Jan 2015
    Posts
    1
    Helped
    0 / 0
    Points
    14
    Level
    1

    Re: Turning two bits into one two-bit binary number

    what if id want to set B to the reverse of A, such at B7 = A0, B6 = A1; while using a for loop. This is using the same program as above. Any help is appreciated.



    •   AltAdvertisement

        
       

  6. #6
    Advanced Member level 4
    Points: 7,811, Level: 21

    Join Date
    Jan 2015
    Posts
    1,095
    Helped
    346 / 346
    Points
    7,811
    Level
    21

    Re: Turning two bits into one two-bit binary number

    If 'A' and 'B' are arrays (i.e. "B7" really means "B[7]" in c-like syntax) then you can use an index into the arrays within the for-loop.
    If they are separate variables, but you can define them in a structure (which gives you come control over their relative addresses) then create a pointer to the start or end of the structures and increment/decrement the pointers within the for-loop.
    Susan



  7. #7
    Super Moderator
    Points: 81,351, Level: 69
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,336
    Helped
    4454 / 4454
    Points
    81,351
    Level
    69

    Re: Turning two bits into one two-bit binary number

    Looking at the simulator it appears 'A' is an 8-bit input port and 'B' is an 8-bit output port in a hypothetical processor. The question is one of adding the 'A' bit pairs together and puting the result on 'B' so it boils down to shifting them to align with the LSB, adding them to a variable (it doesn't say whether 'B' can be used as an accumulator) then outputting the result on 'B'.

    BiscuitDuke - it's your work but try this workflow:
    C = A & 0b00000011;
    C += (A & 0b00001100) >> 2;
    C += (A & 0b00110000) >> 4;
    B = C;

    I show the numbers in binary so you can see how the '&' operator can be used to isolate the '11' bits from the rest of the value in 'A' so only the ones you are interested remain and the other bits are set to zero.

    The '+=' is short hand for "the value on the right is added to the value on the left"

    The '>>' operator shifts the whole 'A' value by the number of places in the number. In my example you can see it is used to move the bit pairs to the rightmost (LSB) places so all three pairs are aligned with each other before adding them.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



  8. #8
    Advanced Member level 4
    Points: 7,811, Level: 21

    Join Date
    Jan 2015
    Posts
    1,095
    Helped
    346 / 346
    Points
    7,811
    Level
    21

    Re: Turning two bits into one two-bit binary number

    I'm sorry, my previous reply was addressed to Micro_Tech90 but I had not realised that he had hijacked the thread. So to Micro_tech90 - use Google as bit reversal in the way you are talking about is a very common operation. Look for bit twiddling and you should find a whole stack of techniques and code.
    Susan



--[[ ]]--