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

A problem to solve..need help

This is 'supposed' to be a simple problem and shouldn't need too much
time to solve :S

Below is a function that works like this:

int fn(int input)
{

int x=(input/8);

if(x%2)
x= x*8 + 8;
else
x = x*8;

return x;

}

Input -> Output
===========
0..7 -> 0
8..15 -> 16
16..23 -> 16
24..31 -> 32
32..39 -> 32
40..47 -> 48
48..56 -> 48

Problem:
See if you can come up with a more efficient solution that can do it
without any division or multiplication operations.

Hint:
Think about what the numbers look like in binary.

If you have no time to look at it, let me know ASAP.

Thanks for your help in advance.
Nov 14 '05 #1
5 1274
go******@hotmail.com (Witless) writes:
int fn(int input)
{

int x=(input/8);

if(x%2)
x= x*8 + 8;
else
x = x*8;

return x;

}

Problem:
See if you can come up with a more efficient solution that can do it
without any division or multiplication operations.


If there is a more efficient solution, then your compiler will
probably come up with it automatically. (If it doesn't, change
the type of `input' to `unsigned' and try again.) It is a waste
of a programmer's time to try to optimize this code.
--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup
Nov 14 '05 #2

"Witless" <go******@hotmail.com> wrote in message
This is 'supposed' to be a simple problem and shouldn't need too much
time to solve :S

int fn(int input)
{

int x=(input/8);

if(x%2)
x= x*8 + 8;
else
x = x*8;

return x;

}

See if you can come up with a more efficient solution that can do it
without any division or multiplication operations.

Hint:
Think about what the numbers look like in binary.

I won't do your homework for you, but think how you would multiply a decimal
number by ten. How would you multiply by 100, or 1000?

As a further hint, make sure you know what the << and >> operators do in C,
also the logical operators (| & ^ and ~), and think about what a binary
number modulus a power of two looks like.
Nov 14 '05 #3
On 10 Jul 2004 11:33:24 -0700, go******@hotmail.com (Witless) wrote in
comp.lang.c:
This is 'supposed' to be a simple problem and shouldn't need too much
time to solve :S

Below is a function that works like this:

int fn(int input)
{

int x=(input/8);

if(x%2)
x= x*8 + 8;
else
x = x*8;

return x;

}

Input -> Output
===========
0..7 -> 0
8..15 -> 16
16..23 -> 16
24..31 -> 32
32..39 -> 32
40..47 -> 48
48..56 -> 48

Problem:
See if you can come up with a more efficient solution that can do it
without any division or multiplication operations.

Hint:
Think about what the numbers look like in binary.

If you have no time to look at it, let me know ASAP.

Thanks for your help in advance.


I agree with Ben, trying to optimize this with type int is rather
silly. What is it supposed to return if you pass it -1?

On the other hand, if you can deal with unsigned numbers, there is
this:

unsigned fn(unsigned int inp)
{
return (inp + 8) & ~(0xfU);
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4
"Witless" <go******@hotmail.com> wrote in message
news:68**************************@posting.google.c om...
This is 'supposed' to be a simple problem and shouldn't need too much
time to solve :S

Below is a function that works like this:

int fn(int input)
{

int x=(input/8);

if(x%2)
x= x*8 + 8;
else
x = x*8;

return x;

}
A compiler must preserve the literal semantics of signed division. Presumably, your
assignment must do the same.

Input -> Output
===========
0..7 -> 0
8..15 -> 16
16..23 -> 16
24..31 -> 32
32..39 -> 32
40..47 -> 48
48..56 -> 48
You haven't said whether this is the entire input range, or merely a sample. That would
dramatically effect the answer.
Problem:
See if you can come up with a more efficient solution that can do it
without any division or multiplication operations.
Chances are, the compiler already does that. Hence Ben's comments.
Hint:
Think about what the numbers look like in binary.
Which is fine if input is always non-negative. It's not so simple for negative input in
which case the value bits could be just about anything under C90. Even under C99 there are
three varieties of negative integer representations. Under C90 there are also two classes
of division when one of the operands is negative!
If you have no time to look at it, let me know ASAP.


[If we didn't have time to look at it, why would we have the time to let you know ASAP
(sic)?]

The nearest ISO C solution I can think of is...

#include <limits.h>

int fn(int input)
{
if (input >= 0)
return input - (input & 7) + (input & 8);

if (INT_MIN + 1 == -INT_MAX && input == INT_MIN)
return INT_MIN;

if (-7 / 8 < 0)
{
input = -1-input;
return -input + (input & 7) - (input & 8);
}

return input + (-input & 15);
}

This still has a compile time test for the class of division which a C90 implementation
might use. I can't see a way around that without using / or %. Note that it's not obvious
that this is a more efficient solution!

Of course, if you make the input an unsigned int, the solution is trivial as Jack has
mentioned.

--
Peter
Nov 14 '05 #5
On 10 Jul 2004 11:33:24 -0700, go******@hotmail.com (Witless) wrote:
This is 'supposed' to be a simple problem and shouldn't need too much
time to solve :S

Below is a function that works like this:

int fn(int input)
{

int x=(input/8);

if(x%2)
x= x*8 + 8;
else
x = x*8;

return x;

}

Input -> Output
===========
0..7 -> 0
8..15 -> 16
16..23 -> 16
24..31 -> 32
32..39 -> 32
40..47 -> 48
48..56 -> 48

Problem:
See if you can come up with a more efficient solution that can do it
without any division or multiplication operations.

Hint:
Think about what the numbers look like in binary.

If you have no time to look at it, let me know ASAP.

Thanks for your help in advance.

int fn(int a)
{if (0<=a && a<=7) return 0;
else if(8<=a && a<=23) return 16;
else if(24<=a && a<=39) return 32;
else return 48;
}

Nov 14 '05 #6

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

Similar topics

2
by: Jet Leung | last post by:
When I debug my program and it return me an error call " have not handle ¡°System.StackOverflowException¡± appear in system.windows.forms.dll " How can I solve this problem??
4
by: MAgnus | last post by:
I need something an algorithm or something else to help me with this function when a and b are given from functions before. (1-(1/(1+r))^b)/r=a this is the function is there anything that can help...
27
by: John Salerno | last post by:
Ok, here's a problem I've sort of assigned to myself for fun, but it's turning out to be quite a pain to wrap my mind around. It's from a puzzle game. It will help if you look at this image: ...
8
by: vj | last post by:
Hi all, I want to solve the two equations u*tan(u)=w and u^2 + w^2=V^2, where V is a known constant, and u and w are the two unknowns to be determined. Please can someone suggest me how to...
5
by: mickey22 | last post by:
Hi, I am getting some errors in building my project I tried to build a C++ file say abc.cpp and I have put the all the directories to include necessary library files and header files..But I...
2
by: katrinkerber | last post by:
Hello, I need help to solve a runtime error that keeps reocurring every time I try to convert an Excel file into an Acess File. I have looked through many forums trying to find help, but I have...
1
by: mostafij | last post by:
// This code write random data into a file and every 5 seconds this //program send 5 seconds data into another file. #include <HEADER FILES> FILE *f1,*f2; int main(int argc, char *argv) {...
7
by: akmaRudiliyn | last post by:
Hai everybody :). I have problem and need help. ERROR: ERROR Violation of PRIMARY KEY constraint 'PK_Table1_01'. Cannot insert duplicate key in object 'dbo.table1'. ERROR The statement has...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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...
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...

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.