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.

mayank.dyl 0 Light Poster

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.

Vish0203 -6 Junior Poster

it can be done using macros.

http://stackoverflow.com/questions/589575/size-of-int-long-etc

this may help you!

Vish0203 -6 Junior Poster

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!!

gusano79 247 Posting Shark

`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?

TrustyTony 888 pyMod Team Colleague Featured Poster

`xor`

and `and`

should be usefull.

Edited by TrustyTony

gusano79 247 Posting Shark

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

mayank.dyl 0 Light Poster

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

TrustyTony 888 pyMod Team Colleague Featured Poster

gusano79 247 Posting Shark

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.

Philippe.Lahaie 42 Posting Whiz in Training

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

mayank.dyl 0 Light Poster

```
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.

TrustyTony 888 pyMod Team Colleague Featured Poster

```
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
```

Edited by TrustyTony

gusano79 247 Posting Shark

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, networking, learning, and sharing knowledge.