Binary AND

I am supposed to write a program that does the following.

Define a binary number as:

int binNum[8];

Use binNum[0] to store the most significant (i.e., leftmost) bit, and binNum[7] the least significant bit. Ask the user to input a binary number with each bit separated by at least one space.
Write the void function

void BinaryAnd (int* bAnd, cons tint* bin1, cons tint* bin2)

to compute bAnd as the AND of the two binary numbers bin1 and bin2. Test your void function with interactive input.

I don't understand how to combine two binary numbers, saved as an integer, in an and statement. Do I need to convert back to an array and deal with that? Otherwise, I am totally lost.
Sounds like you are supposed to use an array to 'mimic' a binary number. The binary AND is used to test a bit, think of it as multiplication. 1*1 = 1, 1*0 = 0, 0*1=0. If you need to do bitwise OR then you do addition. 1 + 0 = 1, 1+ 1 = 1 (I Know its 2 but not in our world) 0 + 1 = 1.
Last edited on
I understand how to do an AND statement, what I don't understand is how to do it with an integer. Do I need to convert the integer into an array again before performing the AND statement? It just sounds stupid. Especially if they entered it with spaced integers specifically to be stored in an array.
Alright well I already gave you the implementation of how a binary AND works. You have been asked to replicate this using integers. Based on what you've said the user is to input 1 0 0 0 1 0 1 0. Each of those are an integer in your array but your AND needs to treat each integer as if it were binary. Well use what I've already stated and your good to go.
Pretty much what clanmjc has said. You are going to need 3 int arrays with 8 members:
int bin1[8], bin2[8], bin3[8];

To commit the AND statement just run the operation for each member in a loop.
1
2
3
4
for (int i = 0; i < 8; ++i) 
{
   bin3[i] = bin1[i] /* Operations here? */ bin[2];
}


That should be inside your binAnd function.
You do not need to convert anything around. You and each bit with its corresponding bit. Luckily with AND and OR you do not need to worry about a carry bit.
What I was confused on is the const int* I took that to be an integer not an array. It makes since if its an array.
Topic archived. No new replies allowed.