By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,905 Members | 1,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,905 IT Pros & Developers. It's quick & easy.

counting of bits

P: n/a

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

May 26 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
<ra******@gmail.comwrote:
Given two integers A & B. Determine how many bits required to convert
A to B.how to write a function int BitSwapReqd(int A, int B);
The exclusive or operator may help you.
May 26 '07 #2

P: n/a
In article <5b*************@mid.individual.net>,
"osmium" <r1********@comcast.netwrote:
<ra******@gmail.comwrote:
Given two integers A & B. Determine how many bits required to convert
A to B.how to write a function int BitSwapReqd(int A, int B);

The exclusive or operator may help you.
Why do I get the feeling someone has a homework assignment, and hasn't
been paying attention in class?

(CF his other post - "function to divide by three")

--
Don Bruder - da****@sonic.net - If your "From:" address isn't on my whitelist,
or the subject of the message doesn't contain the exact text "PopperAndShadow"
somewhere, any message sent to this address will go in the garbage without my
ever knowing it arrived. Sorry... <http://www.sonic.net/~dakiddfor more info
May 26 '07 #3

P: n/a
On May 26, 8:54 pm, Don Bruder <dak...@sonic.netwrote:
In article <5bqulcF2uava...@mid.individual.net>,

"osmium" <r124c4u...@comcast.netwrote:
<rajm2...@gmail.comwrote:
Given two integers A & B. Determine how many bits required to convert
A to B.how to write a function int BitSwapReqd(int A, int B);
The exclusive or operator may help you.

Why do I get the feeling someone has a homework assignment, and hasn't
been paying attention in class?

(CF his other post - "function to divide by three")

--
Don Bruder - dak...@sonic.net - If your "From:" address isn't on my whitelist,
or the subject of the message doesn't contain the exact text "PopperAndShadow"
somewhere, any message sent to this address will go in the garbage without my
ever knowing it arrived. Sorry... <http://www.sonic.net/~dakiddfor more info
how to do this pls help me

May 26 '07 #4

P: n/a
ra******@gmail.com wrote:
On May 26, 8:54 pm, Don Bruder <dak...@sonic.netwrote:
In article <5bqulcF2uava...@mid.individual.net>,

"osmium" <r124c4u...@comcast.netwrote:
<rajm2...@gmail.comwrote:
Given two integers A & B. Determine how many bits required to
convert A to B.how to write a function int BitSwapReqd(int A,
int B);
The exclusive or operator may help you.
Why do I get the feeling someone has a homework assignment, and
hasn't been paying attention in class?

(CF his other post - "function to divide by three")
how to do this pls help me

Sounds like your instructor would like you to write a C program. Better
get going.


Brian
May 26 '07 #5

P: n/a
In article <11**********************@j4g2000prf.googlegroups. com>,
ra******@gmail.com wrote:
On May 26, 8:54 pm, Don Bruder <dak...@sonic.netwrote:
In article <5bqulcF2uava...@mid.individual.net>,

"osmium" <r124c4u...@comcast.netwrote:
<rajm2...@gmail.comwrote:
Given two integers A & B. Determine how many bits required to convert
A to B.how to write a function int BitSwapReqd(int A, int B);
The exclusive or operator may help you.
Why do I get the feeling someone has a homework assignment, and hasn't
been paying attention in class?

(CF his other post - "function to divide by three")

how to do this pls help me
Sure... I can do your homework for you - I charge US$500.00 per half
hour, with a two hour minimum, and expect payment in advance.

Alternatively, you could pay attention in class.

(Hint: If you'd been paying attention, rather than screwing off, you'd
be able to whip out any of these assignments in about 15 minutes, tops.
These are all trivial stuff that anyone can deal with easily - IF they
pay attention in class instead of screwing around.)

--
Don Bruder - da****@sonic.net - If your "From:" address isn't on my whitelist,
or the subject of the message doesn't contain the exact text "PopperAndShadow"
somewhere, any message sent to this address will go in the garbage without my
ever knowing it arrived. Sorry... <http://www.sonic.net/~dakiddfor more info
May 26 '07 #6

P: n/a
In article <11**********************@o11g2000prd.googlegroups .com>,
<ra******@gmail.comwrote:
>Given two integers A & B. Determine how many bits required to convert
A to B.
integers just -are-, independant of representation. Bits are
a particular representation, and there are an infinite number
of bit representations of any given integer.
>how to write a function int BitSwapReqd(int A, int B);
Note that an 'int' is not an integer: int is only an implementation-
defined subset of integers.

Is there supposed to be a connection between the 'Swap' in the
function name and the earlier requirement about "convert A to B" ?
Swapping implies the exchange of two values, while "convert"
only implies the transformation of one value; the two verbs could
come out with very different results.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
May 26 '07 #7

P: n/a
ra******@gmail.com wrote:
>
Given two integers A & B. Determine how many bits required to convert
A to B.how to write a function int BitSwapReqd(int A, int B);
A = B;

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 26 '07 #8

P: n/a
ra******@gmail.com wrote:
Given two integers A & B. Determine how many bits required to convert
A to B.
The number of bits required depends on the definition of conversion in
this instance. What definition are you using?

--
Thad
May 27 '07 #9

P: n/a
On May 27, 2:24 pm, Thad Smith <ThadSm...@acm.orgwrote:
rajm2...@gmail.com wrote:
Given two integers A & B. Determine how many bits required to convert
A to B.

The number of bits required depends on the definition of conversion in
this instance. What definition are you using?
Guess the homework must have been due by now :)

I think it's pretty clear the OP wants to find the "hamming distance"
between the base 2 expression of two integers... I'd do that like
this:

unsigned int hd(unsigned int a, unsigned int b)
{
unsigned int c=a^b, d=0;
while(c) {
d+=(c & 1);
c>>=1;
}
return d;
}

For signed integers, you'd need to worry about how the integer is
being represented - 1s/2s complement etc.

--
Thad

May 27 '07 #10

P: n/a
On May 27, 4:56 pm, Francine.Ne...@googlemail.com wrote:
I think it's pretty clear the OP wants to find the "hamming distance"
between the base 2 expression of two integers... I'd do that like
this:

unsigned int hd(unsigned int a, unsigned int b)
{
unsigned int c=a^b, d=0;
while(c) {
d+=(c & 1);
c>>=1;
}
return d;

}
With slight stylistic improvements (unnecessary auto variable and
superfluous parentheses removed):

unsigned int hd(unsigned int a, unsigned int b)
{
unsigned int d=0;
a^=b;
while(a) {
d+=a & 1;
a>>=1;
}
return d;
}
For signed integers, you'd need to worry about how the integer is
being represented - 1s/2s complement etc.
--
Thad

May 27 '07 #11

P: n/a
Fr************@googlemail.com wrote:
while(a) {
d+=a & 1;
a>>=1;
}
I write that, this way:

while (a != 0) {
++d;
a &= a - 1;
}

--
pete
May 28 '07 #12

P: n/a
Fr************@googlemail.com wrote:
Francine.Ne...@googlemail.com wrote:
>I think it's pretty clear the OP wants to find the "hamming
distance" between the base 2 expression of two integers...
I'd do that like this:

unsigned int hd(unsigned int a, unsigned int b) {
unsigned int c=a^b, d=0;
while(c) {
d+=(c & 1);
c>>=1;
}
return d;
}

With slight stylistic improvements (unnecessary auto variable
and superfluous parentheses removed):

unsigned int hd(unsigned int a, unsigned int b) {
unsigned int d=0;
a^=b;
while(a) {
d+=a & 1;
a>>=1;
}
return d;
}
Additional fooling, to further reduce storage and increase? speed:

#define hd(a, b) hf((a) ^ (b))

unsigned int hf(unsigned int x) {
unsigned int n = 0;

if (x) {
n++;
while (x &= x-1) n++;
}
return n;
} /* untested */

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 28 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.