Thread: Hello Pls solve my problem..How to add,subtract,multiply & divide 2 numbers?without..

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    Hello Pls solve my problem..How to add,subtract,multiply & divide 2 numbers?without..

    Hello,
    how can you add,subtract,multiply and divide 2 numbers without using Arithmatic operators (+,/,*,-,%,++,--)

    i have a basic idea that it can be done using bitwise operator but how that i don't know! can anybody provide me a solution?please?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And why would you do that?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    it's my question! what will i do or not that's a different thing
    if you know you may answer if you don't then fine, even i don't know just wait like me. Let the other big heads come and give the solution

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    i think you should chill your beans a bit matey

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok fine! but is this a question to ask?
    what will you do by that?? what will i do? i can do nothing from that it's just for my information purpose i want to know

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    It's better if you ask clear question to get a clear answer.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is an example of the answer. It's called Bit Shifting. In this example YELLOW, etc., are just defines for numbers, the << is the bit shifting operator, and the 4 is the number of bits that are being shifted. Google that, and you'll be good to go. Might be in the forum FAQ, as well.

    textattr(YELLOW+(RED<<4));
    textattr((CYAN<<4) + RED + BLINK);

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Quote Originally Posted by Adak View Post
    textattr(YELLOW+(RED<<4));
    textattr((CYAN<<4) + RED + BLINK);
    and what for have you used + operator? i dont want any arithmatic operator. Just use bitwise operators like &,~,|,^(And,Complement,Or,XOR)

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you explained why, perhaps someone could better easily find a solution.
    Emulating all the arithmetic operators isn't easy and more complex that it actually solves, so hence the question again, why?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by chottachatri View Post
    and what for have you used + operator? i dont want any arithmatic operator. Just use bitwise operators like &,~,|,^(And,Complement,Or,XOR)
    I wasn't giving a full and complete answer to the question, I believe that was obvious since I mentioned the need to google & check our FAQ.

    The process of learning becomes quite trivialized if you answer every student's question, fully. They have brains, they are problem solvers; a few clues in the right direction is sufficient.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I shall unashamedly link you to the Wikipedia article on binary adders.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    how can you add,subtract,multiply and divide 2 numbers without using Arithmatic operators (+,/,*,-,%,++,--)
    Code:
    int add(int a, int b)
    {
       __asm
       {
          push eax
          mov eax, a
          add b, eax
          pop eax
       }
    
       return b;
    }
    No + sign used...

    By the way, the "++" operator is just a way to say "x = x + 1". It's an addition and an affection operator all in one.

    With the link laserlight posted, you now should have some idea about how to implement addition. Once you done that, other operators will mostly be cake.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No + sign used...
    But you are using an arithmetic instruction. If indeed the list of what is forbidden was exhaustive, then implementing add() with operator+= would be even simpler, heheh.

    It's an addition and an affection operator all in one.
    I have never heard of "affection" used in this context. Do you mean "assignment" instead?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yeah, it's just the assembly equalient of the C/C++ code. I'd call that cheating
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Hey, he asked not to use any arithmetic operator, and i don't see any use of it in the code i gave.

    I have never heard of "affection" used in this context. Do you mean "assignment" instead?
    My mistake, i did mean assignment. Doesn't make a lots of sense: i didn't write correctly the word i was thinking of and even that word doesn't exist in English (it's not my first language).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Basic problem in C++ Pls help
    By anilk in forum C++ Programming
    Replies: 7
    Last Post: 02-09-2006, 09:31 AM
  3. Help to solve this problem
    By Romashka in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2002, 09:32 AM
  4. how to solve this problem???
    By moon in forum C++ Programming
    Replies: 3
    Last Post: 01-21-2002, 03:19 AM