#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("\n enter the elements\n");
scanf("%d%d",&x,&y);
printf("\n before swaping x=%d,y=%d",x,y);
x=x^y;
y=y^x;
x=x^y;
printf("\n after swaping x=%d y=%d",x,y);
getch();
}
Chat with our AI personalities
not 100% sure if this is what you are asking for but here it goes.
you have a number 11110000 (binary) and you want 00001111 (binary)?
you want to use the xor operator ^
value = 0xf0; //11110000 binary
printf("before %d\n");
value ^= 0xff; //11111111 binary
printf("after%d\n");
output:
before 240
after 15
240 in binary is 11110000
15 in binary is 00001111
^
The example provided above gives the illusion that bit order was reversed.
Function revbyte(inbyte As Byte) As Byte
For i = 0 To 7
revbyte= revbyte Or 2 ^ (7 - i) * IIf((inbyte And 2 ^ i) > 0, 1, 0)
Next
End Function
If you have to bits, A and B, then their sum is {A-and-B, A-xor-B} (two bits).
Note: Sometimes you have to add three bits (A, B and C), then the sum is {(A-and-B)-or-(A-and-C)-or-(B-and-C),(A-xor-B)-xor-C} (two bits).
See the attached links for details.
package others;
public class AddTwoNumbers {
private static int myAdd(int a, int b)
{
int carry = a & b;
int result = a ^ b;
while(carry != 0)
{
int shiftedcarry = carry << 1;
carry = result & shiftedcarry;
result ^= shiftedcarry;
}
return result;
}
public static void main(String[] args){
System.out.println(myAdd(4, 5));
System.out.println(myAdd(4, -5));
System.out.println(myAdd(-4, -5));
System.out.println(myAdd(-4, 5));
System.out.println(myAdd(0, 5));
}
}
output :
9
-1
-9
1
5
If you mean, in a computer program, in Python you can swap the value of two variables like this:
a, b = b, a
But in most programming languages, you need an intermediate variable:
temp = a
a = b
b = temp
Without temporary variable:
void swap( int& x, int&y ) { x ^= y ^= x ^= y; }
With temporary variable:
void swap( int&x, int& y ) { int t=x; x=y; y=t; }
Compare the first two numbers with the ternary operator. Store the result in a temporary variable. Compare the temporary variable with the third number, again using the ternary operator.
By using the algorithm of bitwise EORing (Exclusive ORing) the numbers together:If the two numbers are X and Y, then to swap them:X = X EOR YY = Y EOR XX =X EOR Ywill swap them.With knowledge of that algorithm, one then uses the syntax of Javascript to implement it.
The bitwise complement or one's complement operator (~) is used to switch the state of all the bits in a value. Thus 1's become 0, and 0's become 1. One of its many uses is to unset individual bit(s) in a bitmap. We do this with a bitwise AND of the bitmap and the bitwise complement of the bit(s) we want to unset. Original bitmap: 01011100 Bit to unset: 00000100 (e.g., bit 2 (bits are zero based from right)) // Using one's complement and bitwise AND ~00000100 & 01011100 11111011 (one's complement of bit 2) & 01011100 (original bitmap) = 01011000 (original bitmap with bit 2 unset) Note that this formula works even if bit 2 were already unset: 11111011 (one's complement of bit 2) & 01011000 (original bitmap, with bit 2 unset) = 01011000 (original bitmap unchanged)
You cannot compare 2 numbers without using relational operators. Certainly, you could subtract them, but you still need to test the result, and that is a relational operator in itself.
AnswerThe bitwise operators treat a number as its binary equivalent rather than as a simple boolean value.For most programming languages, a value of zero is considered FALSE and all other values are TRUEThus, 8 AND 11 returns TRUE as does 3 OR 0In bitwise analysis, each binary bit of the digit are compared. The number of bits compared will depend on the type of number.In C, a CHAR is usually 8 bits and can hold the binary numbers 0 to 255.If we compare 8 (00001000) and 19 (00010011) with bitwise operators, we get different results from Boolean operators:8 BITWISE AND 19 returns 0 (each bit in the response is set to 1 if both equivalent bits compared are 1) but 8 BITWISE OR 19 will return 27.The utility of these methods is in identifying binary data. For example, all files on a PC have the characteristics 'Hidden' 'Read Only' 'Archive' and 'System' which can be set or unset using bitwise operations on a single byte of data. In truth this is a throwback to the days of small memory capacities where saving the odd byte was essential.There are more uses of bitwise, especially in graphics, where XOR can be used to paint a sprite image to display it and then be used again to return a background to its former settings. I regret I lack the skill to explain this better.