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
}
//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
Get Answers For Free
Most questions answered within 1 hours.