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.
The Problem Write a C or C++ program which performs specific operations on bits. The user...
The Problem Write a C or C++ program which performs specific operations on bits. The user will specify a value The user can then set, test, or toggle individual bits in the initial value. Input Your program will prompt the user for: An inital value in hexadecimal. This value may be up to 32 bits (eight hexadecimal digits) long. If the user-supplied value is less than eight characters in length, assume the additional digits are zero and are on the...
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...
Write a java program "without using Strings using loops and methods "that lets the user to...
Write a java program "without using Strings using loops and methods "that lets the user to enter any two integers and weave them digit by digit and print the result of weaving their digits together to form a single number. Two numbers x and y are weaved together as follows. The last pair of digits in the result should be the last digit of x followed by the last digit of y. The second-to-the-last pair of digits in the result...
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
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 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,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT