473,320 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

counting of bits


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
12 5015
<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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Sam Lowry | last post by:
Greetings. I am trying to do something which should elementary for Perl, but I have only been able to find bits and pieces on it. When I put the bits together they do not work. Maybe I am going...
11
by: Hur | last post by:
hi i ask two questions......someone can tell me i an a linux gcc user and wanna know that - how much physical memory is used for my c code ? and another one is - i need a (standard)...
7
by: sathyashrayan | last post by:
Group, Following function will check weather a bit is set in the given variouble x. int bit_count(long x) { int n = 0; /* ** The loop will execute once for each bit of x set,
7
by: zets | last post by:
I need a macro for counting the bits in the odd positions of a given input (of any type, char, pointer, int, struct, whatever). Is there any clever way I could not think of, to do it efficiently? ...
13
by: John | last post by:
Trying to return the number of 'on' bits in a byte: 'a' = 01100001 = 3 on bits, etc... I'm just adding a bit-wise AND ('&' against octal 1) to a counter called 'bits', then shifting my byte 1...
27
by: Simon Biber | last post by:
I was reading http://en.wikipedia.org/wiki/Poker_probability which has a good description of how to count the frequency of different types of poker hands using a mathematical approach. A sample...
5
by: martin paul | last post by:
Sir please consider the following program.The following program is for counting zero bits till we encounter '1' bit. int main () { unsigned char ch; int i; int count=0;...
3
by: majna | last post by:
I have character counter for textarea wich counting the characters. Special character needs same place as two normal characters because of 16-bit encoding. Counter is counting -2 when special...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.