This is a follow on question from my previously answered question here:
Reading characters on a bit level
The problem that I seem to be having is understanding the basic concepts of bit manipulation in C. I understand what needs to be done but I am having trouble actually developing a solution to it.
An example:
Changing 01101010 to 00111110 using a logical operator.
I understand the principle and the need of a mask to implement the solution but I don’t understand how to do it programatically.
I come from a heavily based C# background and have only recently begun developing in C.
Can anyone give me some pointers or tips to complete the example?
Following is the solution incase anybody else finds it useful:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h.>
int main()
{
//01101010 - 106 Decimal
char a = 0x6A;
//01010100 - 84 Decimal
char b = 0x54;
//00111110 - Uses exclusive or (XOR)
char c = a ^ b;
//Prints out in Hex - 62 Decimal
printf("%x",c);
return 0;
}
You can XOR those together. The XOR of two bits is 1 if exactly one of the bits is 1.
01101010 XOR 00111110 = 01010100XORing the first value with this result will give you the second.
01101010 XOR 01010100 = 00111110In C: