Given two integers A & B. Determine how many bits required to convert A to B. Write a function int BitSwapReqd(int A, int B);

Please first define the logic to calculate the number of bits required to convert A to B and then code.
Thankx a lot.

All 13 Replies

this is not helping.

I'm a starter.. Even i don't know how to implement a macro in a program.. but i'm damn sure that this can be done using macros...

just wait for someone more experienced to reply!!

What does "convert A to B" mean? The function signature `int BitSwapReqd(int A, int B)` tells us A and B are both `int`s, so it's not clear what we're converting here. Do you have a more explicit definition of the problem?

I think it should boil down to number of differing bits in numbers and ther bitwise `xor` and `and` should be usefull.

I think it should boil down to number of differing bits in numbers and ther bitwise xor and and should be usefull.

Probably, but I'm interested in hearing what the OP thinks the task is.

Here convert A to B means that we are assiging value present in variable B to variable A. And they are of type int.

If you are doing swap, you do not need any extra space if you are clever (and same time stupid, as doing tricks to save one variable nowadays is crasy except very special circumstances).

Here convert A to B means that we are assiging value present in variable B to variable A. And they are of type int.

`int BitSwapReqd(int A, int B)` can only return one integer, and it's not going to be able to change the values of `A` and `B` because it's pass by value.

Do you have a more formal description of the problem? The task you posted isn't very clearly written.

also perhaps come up with some evidence of having done some coding yourself! ;)

``````int BitSwapReqd(int A, int B)
``````

takes two int values A and B and returns a int values which suggets that this much number of bits are required to be changed to convert A to B.

The approach may be like that first we convert A and B to binary values then we can take XOR of A and B cuz xor returns 1 for 0 1 or 1 0. Further we should store the xor values in a array from where we can calculate the number of 1's . And this value of number of 1's means that this much number of bits are required to be changed to convert A to B.

Tell me if u ppls agree with this solution or there is any other way to solve this problem.

thnx.

``````while a or b:
different_count += isolate lowest bit by and and xor to find if they differ
shift a and b right

return differing_count
``````

takes two int values A and B and returns a int values which suggets that this much number of bits are required to be changed to convert A to B.

Aha. Now we have a clearer problem statement.

The approach may be like that first we convert A and B to binary values

They're already binary values, no need to convert anything.

then we can take XOR of A and B cuz xor returns 1 for 0 1 or 1 0

Off to a reasonable start...

Further we should store the xor values in a array from where we can calculate the number of 1's .

...but I'm not sure why we think an array is necessary. The value of `A ^ B` is just another `int`.

And this value of number of 1's means that this much number of bits are required to be changed to convert A to B.

Correct.

Now you have an integer value whose bits are 1 where `A` and `B` differ--how would you count those bits?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.