Thread: regular in c

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    2

    Don't use operator in programming language

    Hi all, I want to question way do following exercise
    Enter a number is a, check number a divisible by 3 and put the results on the screen. Don't use operator +,-,*/, %, ++ .... in c++ programming
    Last edited by hoinongdan; 09-16-2014 at 04:30 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Is this a homework assignment? If it is, then your teacher is a jerk. I'm not sure it's even possible to do this without those operators.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    I suppose it might be possible by using some bit-shifting trickery (maybe not ... I haven't really thought about it).

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    The only way I can think of to do it is to use inline assembly, and of course that's not portable, nor is it an elegant solution. I think it's just ridiculous that you're not allowed to use mathematical operators to perform a task that is intrinsically mathematical. You will never encounter a real programming task where you would need to do something like this.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,064
    > Don't use operator +,-,*/, %, ++ .... in c++ programming
    What about += (and all the other assignment operators) ?
    What about -- ?

    The first thing you need to do is look up how addition and subtraction is implemented in computer hardware using a series of AND and OR gates. Then you can synthesise these operations in software. If you can synthesise subtracting 1, you're 90% done.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Can you use fmod()?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Sep 2014
    Posts
    2
    Proplem here is to buildi their operator, so the operator don't use the programming language

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    These operators compile down to single assembly instructions. You can't write anything more fine grained then that. All you'd be doing is doing the same operation 10 times slower.

    If you want to learn how divisibility checks can be implemented in hardware, C++ isn't the language to use. C++ doesn't offer good operators for modifying individual bits. You want a hardware description language like VHDL or Verilog. That would also allow you to implement an operation like this on simulated or real hardware.

    Then of course you need an algorithm to compute the operation (divisibility by 3). A quick google suggests a simple algorithm for doing this given the number in binary (which is how numbers are generally stored).
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by King Mir View Post
    If you want to learn how divisibility checks can be implemented in hardware, C++ isn't the language to use. C++ doesn't offer good operators for modifying individual bits.
    What do you mean by that? Aren't bitwise operators in C++ good enough?

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by antred View Post
    What do you mean by that? Aren't bitwise operators in C++ good enough?
    You could do it, with enough masking, but it's not as convenient as being able to index individual bits. You can hide the masking behind a suitable interface, but your still layering complexity for no good reason.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by King Mir View Post
    You could do it, with enough masking, but it's not as convenient as being able to index individual bits. You can hide the masking behind a suitable interface, but your still layering complexity for no good reason.
    Well, yeah, but wouldn't a higher level language essentially do the same thing? Seeing as the smallest addressable unit is always a byte, they too would have to operate on bytes rather than on individual bits directly.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by antred View Post
    Well, yeah, but wouldn't a higher level language essentially do the same thing? Seeing as the smallest addressable unit is always a byte, they too would have to operate on bytes rather than on individual bits directly.
    Neither Verilog nor VHDL are a higher level languages than C++. They are hardware description languages, used for designing hardware. And in hardware, the smallest addressable unit can easily be a bit.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I don't see any problem with implementing a class for manipulating bits. There's actually one already in the standard library, if you want to use it. it's called std::bitset.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Ok, but I stand by the first part of what I said: "If you want to learn how divisibility checks can be implemented in hardware, C++ isn't the language to use."
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regular Expression
    By stevesmithx in forum C Programming
    Replies: 0
    Last Post: 02-18-2008, 11:00 AM
  2. Help please: regular expressions in C++
    By reivaj7999 in forum C++ Programming
    Replies: 3
    Last Post: 08-24-2005, 01:11 AM
  3. Regular expressions
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-23-2005, 09:36 PM
  4. Regular Expressions
    By Korn1699 in forum C# Programming
    Replies: 4
    Last Post: 01-12-2005, 12:50 AM
  5. regular and non regular languages
    By metsman20 in forum Tech Board
    Replies: 1
    Last Post: 10-19-2003, 10:00 PM