E
E
Eugene Ordinary2016-12-11 07:18:35
Programming
Eugene Ordinary, 2016-12-11 07:18:35

How is unary minus and logical negation implemented?

You need to make a variable with three values: one neutral and two opposite, which can be inverted.
Thought up while such variants:
Values ​​{-1,0,1} and operation of an inversion by a unary minus x=-x.
Values ​​{0,1,2} and xor invert operation x=x^255.
In which case will the inversion be faster?
You also need to make a related variable, but the neutral value is no longer needed, two opposite values ​​\u200b\u200bthat can be inverted are enough. Should I make a bool type or can I take one of the above structures? What is the machine implementation of the negation operation for a bool variable? Does it have any advantages over the above methods?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sitev_ru, 2016-12-11
@sitev_ru

enum Type3Values {t3One, t3Two, t3Three};

class Type3 {
protected:
  Type3Values value;
public:
  Type3();
//Ваши операции
  Type3Values oper1(Type3Values v);
  Type3Values oper2(Type3Values v);
  Type3Values oper3(Type3Values v);
//Операции для bool
  ... oper1(bool v);
  ... oper2(bool v);
  ... oper3(bool v);

};

N
none7, 2016-12-11
@none7

С машинной точки зрения нет особой разницы между bool,char и int, просто первые 2 в памяти обычно занимают меньше места. Также операции -x и x^255 выполняются одинаково быстро, то есть тоже никакой разницы.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question