|
-
Feb 19th, 2002, 05:51 AM
#1
Thread Starter
Junior Member
Logic Required
hi!
actually,i want to write a program in C.the specifications are as below:-
1. Input should be a two digit number(say ab)
2. If a>b then Output -1
3. If a<b then Output +1
4. If a==b then Output 0
5. But we have not to use divide(/) or modulo(%) operator.
6. I think,by using bitwise operators,we may get our goal.But
HOW?
Please give logic behind it??????????????
regards
Rohit..
-
Feb 19th, 2002, 06:24 AM
#2
why get so complicated? it's a very small program and any
increase in speed (if any) will be insignificant.
just use normal equality expressions (==, >, <)
-
Feb 19th, 2002, 06:42 AM
#3
Junior Member
You can mutiply adding up shifts e.g.
x * 10 = x<<1 + x<<3
<<1 (*2)
<<2 (*4)
<<3 (*8)
<<4 (*16)
etc.
-
Feb 19th, 2002, 09:20 AM
#4
The problem is seperating A and B. The ususal way is using / and % with 10, but this is forbidden.
You could use hex digits and claim that they are digits too. Then it is easy:
a = ab >> 4;
b = ab & 0x0f;
Decimal digits are far harder.
it takes a lot of bitmask and shifting (I can't think of a way), or you can simulate the operation a cpu does when dividing: substract and count
Code:
// do a division and modulo at the same time
int ab = GetInput();
int temp = ab;
int cnt = 0;
int result = -2; // error by default
while(temp >= 10)
{
temp -= 10;
++cnt;
}
// now cnt == ab / 10, temp == ab % 10
if(cnt == ab) result = 0;
if(cnt < ab) result = -1;
if(cnt > ab) result = 1;
return result;
That's a dirty trick however, and may be forbidden.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 19th, 2002, 01:01 PM
#5
transcendental analytic
Cheating now hehe, Msvc7 came up with this solution:
PHP Code:
00013 b8 cd cc cc cc mov eax, -858993459 ; cccccccdH
00018 f7 64 24 0c mul DWORD PTR _x$[esp+16]
0001c c1 ea 03 shr edx, 3
so in C++ that would be:
PHP Code:
unsigned int x,y;
cin>>x;
x-=10*(y=(x*0xcccd)>>19);
if(y == x) result = 0;
if(y < x) result = -1;
if(y > x) result = 1;
cout<<result;
Last edited by kedaman; Feb 19th, 2002 at 01:36 PM.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Feb 19th, 2002, 05:01 PM
#6
so the workaround is the same as:
x / 10 == x * (m/10) / m
and we are searching for an m that is a power of 2 and can be divided by 10 evenly. There is no such number, but at 0xcccd, which is 52429, the difference to 2^19 (= 524288) is so small compared to the magnitude of the numbers, that the relatively unprecise shift-right division will come up with the right result.
MSVC 7 is really impressive. That thing must be possible with many numbers.
There should be some optimization like "optimize calls to pow, sqrt, sin etc. with constant parameters away". This would preserve code readability without the necessity of compile time template calculations, yet cause no impact on runtime.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 19th, 2002, 06:36 PM
#7
transcendental analytic
There should be some optimization like "optimize calls to pow, sqrt, sin etc. with constant parameters away". This would preserve code readability without the necessity of compile time template calculations, yet cause no impact on runtime.
Might be, I haven't tried out yet, that would quite beat the purpose of templates in a lot of cases indeed in that case it could threaten BORK but it has a lot of other intuitive features, which a C++ programmer can't dream of
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|