Question

Write a program in C that xor:s two numbers without using the built-in xor bool xor_bit...

Write a program in C that xor:s two numbers without using the built-in xor


bool xor_bit ( bool a , bool b ) {

// xor two bits , without using ^.

}

uint32_t xor_word ( uint32_t a , uint32_t b) {

// xor two words , without using ^; instead call xor_bit for every bit .

}

int main () {

uint_32 a = 0xaabbccdd ;

uint_32 b = 0 x11223344 ;

// xor a and b by calling xor_word

//Print out the result as a hexadecimal number

}

Homework Answers

Answer #1

//C program

#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
bool xor_bit ( bool a , bool b ) {

   return (a & b) ? 0 : (a | b);

}

uint32_t xor_word ( uint32_t a , uint32_t b) {
   uint32_t result = 0;
   int i;
   for (i = 31; i >= 0; i--)
{

bool b1 = a & (1 << i);
bool b2 = b & (1 << i);

bool xoredBit = xor_bit ( b1 , b2 ) ;   
  
result <<= 1;
result |= xoredBit;
}
return result;

}

int main () {

uint32_t a = 0xaabbccdd ;

uint32_t b = 0x11223344 ;

uint32_t result = xor_word ( a , b);

printf("%x\n",result);

return 0;

}

//sample output

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The bitwise operators are very similar to the ones that you might remember from Java. They...
The bitwise operators are very similar to the ones that you might remember from Java. They are used a lot more frequently in C, though, because C is mostly chosen when you want to write low level programs. A quick reminder: & (bitwise AND). Takes two numbers as operand and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1. | (bitwise OR). Takes two numbers as operand and does...
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers....
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers. The data for this program will be entered from the keyboard using JOptionPane one 16-bit binary number at a time. Note that each base 2 number is actually read in as a String. The program should continue until a 16-bit base 2 number consisting of all 0’s is entered. Once the 16-bit number has been entered your program should make sure that the input...
C++ PROGRAM. (C++ INTRO QUESTION) Write a program that prints the count of all prime numbers...
C++ PROGRAM. (C++ INTRO QUESTION) Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = 55000 B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
Write an assembly language program that reverses all the bits of a 32-bit number. (Without using...
Write an assembly language program that reverses all the bits of a 32-bit number. (Without using RBIT instruction) Write an assembly language program that checks whether an unsigned number is perfect square or not.
Create a program to ask the user for an integer number, positive only. If the number...
Create a program to ask the user for an integer number, positive only. If the number is between 0 and 255, printout the number in binary format to the screen. This will involve checking the bits, one by one, starting from the most significant place: 128. Use c++ 1. Modify the program to find the binary number, using a FOR loop, instead of the manual checking of each bit separately 2. What are the bitwise XOR and INVERSION operators? Find...
PYTHON 3 Write a program that prints the count of all prime numbers between A and...
PYTHON 3 Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = 21212 B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules: You should first...
Write a program that asks the user to enter two integer numbers. the main function then...
Write a program that asks the user to enter two integer numbers. the main function then passes these numbers to a function named exp that calculates the multiplication of these numbers and print the exponential value of the result of multiplication
Write a program in python that prints the count of all prime numbers between A and...
Write a program in python that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = The 5 digit unique number you had picked at the beginning of the semester B = A + 5000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2,...
In C++ Please, In the main(), below, write a program that uses a switch-statement to choose...
In C++ Please, In the main(), below, write a program that uses a switch-statement to choose between three numbers. The number, choice, should be prompted for and each choice should print out a unique statement. Also, the prompt should continue indefinitely (i.e. keep prompting until the user force-quits the program), and there should be a general catch-all if the user enters other than the three possible choices. #include <iostream> #include <iomanip> using namespace std; int main() { <your answer goes...
Using a for Loop visual c++ Summary In this lab the completed program should print the...
Using a for Loop visual c++ Summary In this lab the completed program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. You should accomplish this using a for loop instead of a counter-controlled while loop. Instructions Write a for loop that uses the loop control variable to take on the values 0 through 10. In the body of the loop, multiply the value of the loop control variable by 2 and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT