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

PROBLEM...please READ

//Problem Statement
//
//You work for a company that sells mechanical bit counting devices.
These devices
//wear out when they do a lot of counting. You are going to see how
much wear has
//been put on one particular 15-bit binary counter. One unit of wear
occurs for
//every bit that is flipped in the counter.
//
//For example:
//If the counter was at 7 = 000000000000111
//And it was incremented to 8 = 000000000001000
//
//Then 4 bits are flipped so 4 units of wear would occur. If a counter
was
//incremented through an inclusive range like 7 -> 12 then :
//Starts at 7 = 000000000000111
//Goes to 8 = 000000000001000 7 -> 8 = 4 units of wear
//Goes to 9 = 000000000001001 8 -> 9 = 1 unit of wear
//Goes to 10 = 000000000001010 9 -> 10 = 2 units of wear
//Goes to 11 = 000000000001011 10 -> 11 = 1 unit of wear
//Goes to 12 = 000000000001100 11 -> 12 = 3 units of wear
//This causes a total of 4+1+2+1+3 = 11 units of wear. Return the total
wear caused by incrementing the counter through the inclusive range
between start and finish.
//Definition
//
//Class: MechanicalCounter
//Method: amountWear
//Parameters: int, int
//Returns: int
//Method signature: int amountWear(int start, int finish)
//
//(be sure your method is public)
//
//
//Constraints
//-start will be less than finish.
//-start will be between 0 and 32766 inclusive.
//-finish will be between 1 and 32767 inclusive.
//
//Examples
//0)
//7
//8
//Returns: 4
//As seen above, 4 units of wear occur when 7 becomes 8.
//
//1)
//
//7
//12
//Returns: 11
//The example in the problem statement.
//
//2)
//1
//32767
//Returns: 65518

Mar 13 '06 #1
11 1656
"Gagan" <In*******@gmail.com> writes:
//Problem Statement
//
//You work for a company that sells mechanical bit counting devices.

[snip]

No, I don't, so I guess I can't help you.

This looks very much like a homework problem, and you've made no
visible effort to solve it yourself.

If you really want us to do all the work for you, please give us your
instructor's e-mail address so we can submit the solution directly.
If not, show us some code, and we might be able to help.

--
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.
Mar 13 '06 #2
In article <11*********************@p10g2000cwp.googlegroups. com>,
Gagan <In*******@gmail.com> wrote:
//Class: MechanicalCounter
//Method: amountWear //Method signature: int amountWear(int start, int finish)


Methods do not exist in C. If you have a C++ question, you need to
ask it in comp.lang.c++ .

But it doesn't appear that you have a C++ question: it appears that
you have a homework problem and it appears that your implicit
question is whether someone will do your homework for you and allow
you to claim the credit.

If you have an -algorithm- question, then one of the programming
newsgroups might be appropriate -- but they are not likely to
help you unless you give an outline of your thinking on the
topic, and you give -specific- questions about the parts you
are unsure about.
I will go as far as to give you a hint: think about "exclusive or".
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Mar 13 '06 #3
>>Problem Statement
You work for a company that sells mechanical bit counting devices.
These devices


I think you have just copied and pasted your assignment problem. No
body in this group will do the whole assignment for you. Its better if
you sit down and start solving it by yourself and then if you face any
difficulties you can paste it here.

Vishal

Mar 13 '06 #4
Gagan wrote:
Return the total wear caused by incrementing the counter through
the inclusive range between start and finish.


Hint: consider popcount(i XOR i+1)
Mar 13 '06 #5

Gagan wrote:
//Problem Statement
//
//You work for a company that sells mechanical bit counting devices.
These devices
//wear out when they do a lot of counting. You are going to see how
much wear has
//been put on one particular 15-bit binary counter. One unit of wear
occurs for
//every bit that is flipped in the counter.
//
//For example:
//If the counter was at 7 = 000000000000111
//And it was incremented to 8 = 000000000001000
//
//Then 4 bits are flipped so 4 units of wear would occur. If a counter
was
//incremented through an inclusive range like 7 -> 12 then :
//Starts at 7 = 000000000000111
//Goes to 8 = 000000000001000 7 -> 8 = 4 units of wear
//Goes to 9 = 000000000001001 8 -> 9 = 1 unit of wear
//Goes to 10 = 000000000001010 9 -> 10 = 2 units of wear
//Goes to 11 = 000000000001011 10 -> 11 = 1 unit of wear
//Goes to 12 = 000000000001100 11 -> 12 = 3 units of wear
//This causes a total of 4+1+2+1+3 = 11 units of wear. Return the total
wear caused by incrementing the counter through the inclusive range
between start and finish.
//Definition
//
//Class: MechanicalCounter
//Method: amountWear
//Parameters: int, int
//Returns: int
//Method signature: int amountWear(int start, int finish)
//
//(be sure your method is public)
//
//
//Constraints
//-start will be less than finish.
//-start will be between 0 and 32766 inclusive.
//-finish will be between 1 and 32767 inclusive.
//
//Examples
//0)
//7
//8
//Returns: 4
//As seen above, 4 units of wear occur when 7 becomes 8.
//
//1)
//
//7
//12
//Returns: 11
//The example in the problem statement.
//
//2)
//1
//32767
//Returns: 65518


<OT>
In the same practise room.look at the room summary and view how
others solved that.
</OT>

Mar 13 '06 #6
"Gagan" writes:
//Problem Statement
//
//You work for a company that sells mechanical bit counting devices.
These devices
//wear out when they do a lot of counting. You are going to see how
much wear has
//been put on one particular 15-bit binary counter. One unit of wear
occurs for
//every bit that is flipped in the counter.

<snip>

Using pencil and paper make a truth table for the binary representation of n
for 0 to 15 in column one. Make column two the exclusive or of n and n+1.
Look for interesting relationships. Continue.
Mar 13 '06 #7
On 2006-03-13, Gagan <In*******@gmail.com> wrote:
//If the counter was at 7 = 000000000000111
//And it was incremented to 8 = 000000000001000
//
//Then 4 bits are flipped so 4 units of wear would occur. If a counter


Looks like a college assignment so no complete answer. It also
mentioned your routine being "public" : are your sure you want C
and not C++?

Some guidelines to help you search for a solution:

1) What is it you are trying to find at a basic level? A) The number
of bits which have *changed*.
2) How do you do that? A) Use a bitwise exclusive or (XOR).
3) When you can see all the changed bits, what do you need to do next?
A) Count them. This might involve individual bit checks using
bitwise AND operation (&); Find examples in your C book or using google.

Good luck!

(Note this is the basic solution : the question asks for more than
that - it asks for the accumulation of changed bits when clicking from
"start" to "end" : this needs more thinking about).
Mar 13 '06 #8
"Richard G. Riley" <rg****@gmail.com> writes:
On 2006-03-13, Gagan <In*******@gmail.com> wrote:
//If the counter was at 7 = 000000000000111
//And it was incremented to 8 = 000000000001000
//
//Then 4 bits are flipped so 4 units of wear would occur. If a counter


Looks like a college assignment so no complete answer. It also
mentioned your routine being "public" : are your sure you want C
and not C++?

Some guidelines to help you search for a solution:

1) What is it you are trying to find at a basic level? A) The number
of bits which have *changed*.
2) How do you do that? A) Use a bitwise exclusive or (XOR).
3) When you can see all the changed bits, what do you need to do next?
A) Count them. This might involve individual bit checks using
bitwise AND operation (&); Find examples in your C book or using google.


There's a neat little trick for counting bits that I happened upon
recently. It's appealing because it's also divide-and-conquer. I won't
post an implementation, but... note that an octet could be considered
equivalent to eight 1-bit counts of how many bits could be found in
each bit position (always either zero or one). If you add the value of
each even bit position to the value of each odd position
(((value & 0xAA) >> 1) + (value & 0x55)), you'll end up with an octet
with four 2-bit counts of how many bits existed in each original
2-bit field. You can then add these 2-bit fields together to get 4-bit
counts, etc, until you have a count for the entire octet. And, of
course, you can expand this to work for 16-bit values, or whatever
else you need.

I ran across this while reading the source code for an implementation
of Minesweeper that guarantees to always generate solvable puzzles,
written by Simon Tatham (who also wrote the PuTTY terminal emulator
and telnet/ssh client). I asked him about it, and he'd forgotten where
he'd first seen it, but mentioned he'd seen it recently in the book
"Hacker's Delight", which is filled with similar bit-manipulation
techniques.
Mar 14 '06 #9
On 2006-03-14, Micah Cowan <mi***@cowan.name> wrote:
"Richard G. Riley" <rg****@gmail.com> writes:
On 2006-03-13, Gagan <In*******@gmail.com> wrote:
> //If the counter was at 7 = 000000000000111
> //And it was incremented to 8 = 000000000001000
> //
> //Then 4 bits are flipped so 4 units of wear would occur. If a counter
Looks like a college assignment so no complete answer. It also
mentioned your routine being "public" : are your sure you want C
and not C++?

Some guidelines to help you search for a solution:

1) What is it you are trying to find at a basic level? A) The number
of bits which have *changed*.
2) How do you do that? A) Use a bitwise exclusive or (XOR).
3) When you can see all the changed bits, what do you need to do next?
A) Count them. This might involve individual bit checks using
bitwise AND operation (&); Find examples in your C book or using google.


There's a neat little trick for counting bits that I happened upon
recently. It's appealing because it's also divide-and-conquer. I won't
post an implementation, but... note that an octet could be considered
equivalent to eight 1-bit counts of how many bits could be found in
each bit position (always either zero or one). If you add the value of
each even bit position to the value of each odd position
(((value & 0xAA) >> 1) + (value & 0x55)), you'll end up with an octet
with four 2-bit counts of how many bits existed in each original
2-bit field. You can then add these 2-bit fields together to get 4-bit
counts, etc, until you have a count for the entire octet. And, of
course, you can expand this to work for 16-bit values, or whatever
else you need.


A 256-element lookup table would almost definitely be faster.
I ran across this while reading the source code for an implementation
of Minesweeper that guarantees to always generate solvable puzzles,
written by Simon Tatham (who also wrote the PuTTY terminal emulator
and telnet/ssh client). I asked him about it, and he'd forgotten where
he'd first seen it, but mentioned he'd seen it recently in the book
"Hacker's Delight", which is filled with similar bit-manipulation
techniques.

Mar 14 '06 #10
Jordan Abel <ra*******@gmail.com> writes:
On 2006-03-14, Micah Cowan <mi***@cowan.name> wrote:
There's a neat little trick for counting bits that I happened upon
recently. It's appealing because it's also divide-and-conquer. I won't
post an implementation, but... note that an octet could be considered
equivalent to eight 1-bit counts of how many bits could be found in
each bit position (always either zero or one). If you add the value of
each even bit position to the value of each odd position
(((value & 0xAA) >> 1) + (value & 0x55)), you'll end up with an octet
with four 2-bit counts of how many bits existed in each original
2-bit field. You can then add these 2-bit fields together to get 4-bit
counts, etc, until you have a count for the entire octet. And, of
course, you can expand this to work for 16-bit values, or whatever
else you need.


A 256-element lookup table would almost definitely be faster.


Naturally. It would also require much more storage. Certainly the
lookup table is a better solution if you're going to be doing this
over and over again in a tight loop; but I wonder if it's worth it if
you're only doing it occaisionally? *Shrug*, depends on the application.
Mar 15 '06 #11
On 12 Mar 2006 19:37:42 -0800, "Gagan" <In*******@gmail.com> wrote:
//Problem Statement
//
//You work for a company that sells mechanical bit counting devices.
These devices
//wear out when they do a lot of counting. You are going to see how
much wear has
//been put on one particular 15-bit binary counter. One unit of wear
occurs for
//every bit that is flipped in the counter.


Advise them to switch to Grey coding. Saves wear, and also makes it
harder for first-term students to cheat on their homework.

- David.Thompson1 at worldnet.att.net
Mar 20 '06 #12

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

Similar topics

38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
10
by: crawlerxp | last post by:
This is the problem: I do not get the output I need when encoding and decoding data using rijndael alghoritm. Look at the code and see what the problem is actually: Please paste this code into...
3
by: engartte | last post by:
Hi all, I did the solution with consultation of some experts on the following problem: Declare an array of length of 10 and read integers into the elements of the array from keyboard.Then...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
0
by: Harley | last post by:
Hello, I am just learning the tcp/ip functions etc under vb.net so please look over me if this is obviouse. I have been all over looking into any functions that I didn't totaly understand and...
5
by: Segfahlt | last post by:
I need a little help here please. I have 2 win forms user controls in 2 different projects that I'm hosting in 2 different virtual directories. The controls have been test and operate okay in...
2
by: almurph | last post by:
Hi everyone, Can you help me please? I am having a problem with the encryption/decryption of words with the Irish fada in them. The Irish fada is like this: αινσϊ/ΑΙΝΣΪ. It's kind of like the...
5
by: Bo Yang | last post by:
Hi , I have writen a python program to slove a problem described as below: (Forgive my poor English !) Put the 2^n 0 or 1 to form a ring , and we can select any continuous n ones from the...
21
by: Rajen | last post by:
When I run this program. #include<stdio.h> int main() { FILE *fp; int i; float f; char str;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.