answersLogoWhite

0

#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();

}

User Avatar

Wiki User

12y ago

Still curious? Ask our experts.

Chat with our AI personalities

LaoLao
The path is yours to walk; I am only here to hold up a mirror.
Chat with Lao
SteveSteve
Knowledge is a journey, you know? We'll get there.
Chat with Steve
EzraEzra
Faith is not about having all the answers, but learning to ask the right questions.
Chat with Ezra
More answers

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

User Avatar

Wiki User

12y ago
User Avatar

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.

User Avatar

Wiki User

16y ago
User Avatar

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

User Avatar

Wiki User

14y ago
User Avatar

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

User Avatar

Wiki User

11y ago
User Avatar

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; }

User Avatar

Wiki User

11y ago
User Avatar

a := a XOR b

b := a XOR b

a := a XOR b

User Avatar

Wiki User

13y ago
User Avatar

Add your answer:

Earn +20 pts
Q: How do you subtraction two numbers using bitwise operator?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you find the greatest of three numbers using ternery operator?

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.


How do you write a javascript to swap two numbers without using third one?

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.


What is the use of complementry operator in c?

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 &amp; 01011100 11111011 (one's complement of bit 2) &amp; 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) &amp; 01011000 (original bitmap, with bit 2 unset) = 01011000 (original bitmap unchanged)


How do you compare two numbers without using any operators in c?

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.


What is the use of Bitwise operators?

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.