+ Post New Thread
Results 1 to 8 of 8

4th September 2014, 16:56 #1
 Join Date
 Sep 2014
 Posts
 13
 Helped
 0 / 0
 Points
 283
 Level
 3
Turning two bits into one twobit binary number
Hello everyone. I need help on a homework assignment:
Write a C program that treats A1A0, A3A2, and A5A4 as three 2bit unsigned binary
number. The program should output the sum of those three numbers onto B.
I need help with the 'treat A1A0... 2bit 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 twobit number.
Thank you in advance.

Advertisement

4th September 2014, 17:09 #2
 Join Date
 Nov 2008
 Location
 Norwich, UK
 Posts
 2,123
 Helped
 598 / 598
 Points
 12,735
 Level
 27
Re: Turning two bits into one twobit 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

4th September 2014, 22:35 #3
 Join Date
 Sep 2014
 Posts
 13
 Helped
 0 / 0
 Points
 283
 Level
 3

Advertisement

5th September 2014, 06:27 #4
 Join Date
 Nov 2008
 Location
 Norwich, UK
 Posts
 2,123
 Helped
 598 / 598
 Points
 12,735
 Level
 27
Re: Turning two bits into one twobit 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;

23rd January 2015, 00:04 #5
 Join Date
 Jan 2015
 Posts
 1
 Helped
 0 / 0
 Points
 14
 Level
 1
Re: Turning two bits into one twobit 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.

23rd January 2015, 02:41 #6
 Join Date
 Jan 2015
 Posts
 1,095
 Helped
 346 / 346
 Points
 7,811
 Level
 21
Re: Turning two bits into one twobit binary number
If 'A' and 'B' are arrays (i.e. "B7" really means "B[7]" in clike syntax) then you can use an index into the arrays within the forloop.
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 forloop.
Susan

23rd January 2015, 11:02 #7
Awards:
 Join Date
 Jul 2009
 Location
 Aberdyfi, West Wales, UK
 Posts
 13,331
 Helped
 4453 / 4453
 Points
 81,321
 Level
 69
Re: Turning two bits into one twobit binary number
Looking at the simulator it appears 'A' is an 8bit input port and 'B' is an 8bit 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.

Advertisement

24th January 2015, 03:44 #8
 Join Date
 Jan 2015
 Posts
 1,095
 Helped
 346 / 346
 Points
 7,811
 Level
 21
Re: Turning two bits into one twobit 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
+ Post New Thread
Please login