473,322 Members | 1,352 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,322 software developers and data experts.

bits required

count the bits required to be altered while swaping values a and b

May 26 '07 #1
11 1576
ra******@gmail.com said:
count the bits required to be altered while swaping values a and b
Why?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 26 '07 #2
ra******@gmail.com wrote:
count the bits required to be altered while swaping values a and b

You can repost it all you want, we still won't do your homework for
you.

Back in the day, when I was a teaching assistant (Physics), I used to
tell the students, "It's pointless to cheat on homework. Homework is
for you. We only collect and grade it so you'll get feedback on what
you're doing wrong and to give you incentive to actually do the work.
Exams are where we find out what you really know."


Brian
May 26 '07 #3
In article <11**********************@a26g2000pre.googlegroups .com>,
<ra******@gmail.comwrote:
>count the bits required to be altered while swaping values a and b
The answer may be ill-defined. If a temporary value is used,
the the number of bits required to be altered to set that
temporary to a certain value would depend on what was in
the temporary to start with, which is unknown if the temporary
is in automatic storage.

There is also the question of how to count the alteration of bits.
If a particular bit gets altered twice, then is that a count of 2
(two alterations), or a count of 1 (one bit altered) ?

Is the requirement to come up with a formula that expresses the
number of bits required to be altered to swap two values? Or is
the requirement to come up with a program that would count the
bits required to be altered to swap two values that are inputs?
Or are the two values given ahead of time?

What do we know about the types of the two values a and b?
Are they integral types? Are they potentially floating point
numbers? Are they potentially of mixed type, one integral
and one floating point?

What are we to do about the problem that "bits" are a matter
of representation (which is not completely defined in C),
whereas "values" are independant of representation? For example,
the answer might be different if the C implementation happens
to be using two's complement to represent values when the programmer
is expecting one's complement representation instead.
--
Programming is what happens while you're busy making other plans.
May 26 '07 #4
On 26 May 2007 09:11:23 -0700, ra******@gmail.com wrote:
>count the bits required to be altered while swaping values a and b
Posting the same question every hour will not get you more or faster
responses. However, it will increase the number of killfiles that
contain you name.

Posting an obvious homework question while making no attempt to solve
it yourself merely hastens the process.
Remove del for email
May 26 '07 #5
Barry Schwarz said:
On 26 May 2007 09:11:23 -0700, ra******@gmail.com wrote:
>>count the bits required to be altered while swaping values a and b

Posting the same question every hour will not get you more or faster
responses. However, it will increase the number of killfiles that
contain you name.
Indeed. In fact, from over here it's already too late.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 26 '07 #6
ra******@gmail.com wrote:
count the bits required to be altered while swaping values a and b
42.7
--
Flash Gordon
May 26 '07 #7
ra******@gmail.com writes:
count the bits required to be altered while swaping values a and b
Give us your instructor's e-mail address so we can submit the solution
directly.

Optionally, you can also give us your name so we can tell your
instructor who should get the "credit".

Or you might just consider doing your own homework.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 26 '07 #8
ra******@gmail.com wrote:
>
count the bits required to be altered while swaping values a and b
/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

unsigned bit_count(unsigned n);

int main(void)
{
unsigned a, b, mask;

a = rand();
b = rand();
mask = a ^ b;
printf("a is %u\nb is %u.\n", a, b);
printf("%u bits would need to be flipped in each object\n"
"to swap their values.\n", bit_count(mask));
printf("%u ^ %u is %u\n", a, mask, a ^ mask);
printf("%u ^ %u is %u\n", b, mask, b ^ mask);
return 0;
}

unsigned bit_count(unsigned n)
{
unsigned count;

for (count = 0; n != 0; n &= n - 1) {
++count;
}
return count;
}

/* END new.c */

--
pete
May 26 '07 #9
On May 27, 3:52 am, pete <pfil...@mindspring.comwrote:
rajm2...@gmail.com wrote:
count the bits required to be altered while swaping values a and b

/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

unsigned bit_count(unsigned n);

int main(void)
{
unsigned a, b, mask;

a = rand();
b = rand();
mask = a ^ b;
printf("a is %u\nb is %u.\n", a, b);
printf("%u bits would need to be flipped in each object\n"
"to swap their values.\n", bit_count(mask));
printf("%u ^ %u is %u\n", a, mask, a ^ mask);
printf("%u ^ %u is %u\n", b, mask, b ^ mask);
return 0;

}

unsigned bit_count(unsigned n)
{
unsigned count;

for (count = 0; n != 0; n &= n - 1) {
++count;
}
return count;

}

/* END new.c */

--
pete
Why you are using unsigned int everywhere?Why count is unsigned?And
why you XOR the inputs?

May 27 '07 #10
Shraddha wrote:
>
On May 27, 3:52 am, pete <pfil...@mindspring.comwrote:
rajm2...@gmail.com wrote:
count the bits required to be altered while swaping values a and b
/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

unsigned bit_count(unsigned n);

int main(void)
{
unsigned a, b, mask;

a = rand();
b = rand();
mask = a ^ b;
printf("a is %u\nb is %u.\n", a, b);
printf("%u bits would need to be flipped in each object\n"
"to swap their values.\n", bit_count(mask));
printf("%u ^ %u is %u\n", a, mask, a ^ mask);
printf("%u ^ %u is %u\n", b, mask, b ^ mask);
return 0;

}

unsigned bit_count(unsigned n)
{
unsigned count;

for (count = 0; n != 0; n &= n - 1) {
++count;
}
return count;

}

/* END new.c */
Why you are using unsigned int everywhere?
Why count is unsigned?
Bitwise operations are better suited to unsigned types.
And why you XOR the inputs?
The result of a ^ b,
is a mask which has the bits set,
where a and b differ.

--
pete
May 27 '07 #11

"Shraddha" <sh*************@gmail.comschrieb im Newsbeitrag
news:11*********************@j4g2000prf.googlegrou ps.com...
On May 27, 3:52 am, pete <pfil...@mindspring.comwrote:
>rajm2...@gmail.com wrote:
count the bits required to be altered while swaping values a and b

/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

unsigned bit_count(unsigned n);

int main(void)
{
unsigned a, b, mask;

a = rand();
b = rand();
mask = a ^ b;
printf("a is %u\nb is %u.\n", a, b);
printf("%u bits would need to be flipped in each object\n"
"to swap their values.\n", bit_count(mask));
printf("%u ^ %u is %u\n", a, mask, a ^ mask);
printf("%u ^ %u is %u\n", b, mask, b ^ mask);
return 0;

}

unsigned bit_count(unsigned n)
{
unsigned count;

for (count = 0; n != 0; n &= n - 1) {
++count;
}
return count;

}

/* END new.c */

--
pete

Why you are using unsigned int everywhere?
To avoid a leading bit to be interpreted as a negative value
Why count is unsigned?
Why not? After all there would never be a negative number of bits counted.
unsigned char would have been sufficient here, don't think there are many
machine out there with more than 256 bits in an int...
And why you XOR the inputs?
To find the bits that are set differently in the 2 operands

Bye, Jojo
May 27 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Ben | last post by:
Hi, I need to write some data types into an array of unsigned chars. These are basically "signals" within a message, so each signal will have a start bit and a length. The signals will also...
40
by: aku | last post by:
I'm looking for the absolute fastest way to count the nr of bits that are set to "1" in a string. Presumably I then first need the fastest way to do this in a byte. I think this is it, but...
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,
0
by: Gent | last post by:
Does BITS (Background Intelligent Transfer Service) 1.5 require Dot Net framework on the client side. From what I have read it seems like Bits 2.0 needs the dot net framework but Bits 1.5 does not....
4
by: Lance | last post by:
I have an array of bytes that I need to convert into an array of Integers. But, the number of bits per value in the Byte array is not necessarily divisible by 8 (although it will never exceed...
64
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? -...
25
by: Daniel Kraft | last post by:
Hi, I do need to implement something similar to C++'s std::bitset in C; for this, I use an array of int's to get together any desired number of bits, possibly larger than 32/64 or anything like...
6
by: John Messenger | last post by:
I notice that the C standard allows padding bits in both unsigned and signed integer types. Does anyone know of any real-world examples of compilers that use padding bits? -- John
2
by: James Harris | last post by:
I'm trying to make sense of the standard C data sizes for floating point numbers. I guess the standards were written to accommodate some particular floating point engines that were popular at one...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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: 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.