-1

I was practicing for all the logic gate implementations for bits and then I started to implement NOR but I don't know why it is giving negative values for using '~' operator. It did give the proper results when I used '!' operator. I am not able to find the logic behind what is wrong with what I am doing.

I read through operators in C++ and clearly mentions that to reverse a bit using '~' operator and '!' for logical operations

void NOR(vector<string>& input){ // input is string of vectors with values 0 0, 0 1, 1 0, 1 1

cout<<"---------------------------------"<<endl;

  for(auto& i : input){

    cout<<"NOR of "<<i[0]<<" "<<i[1];

    cout<<" is "<<~((i[0]-'0') | (i[1]-'0'))<<endl;

  }

 cout<<"---------------------------------"<<endl;

}

The results which I am getting is

NOR of 0 0 is -1

NOR of 1 0 is -2

NOR of 0 1 is -2

NOR of 1 1 is -2

1 Answers1

2

C++ doesn't have a logical XOR operator (only a bitwise XOR: ^).

As for NOR - C++ doesn't have any kind of NOR implemented.

You can simply use:

template <typename I>
constexpr I logical_nor(I lhs, I rhs) noexcept { return not (lhs or rhs); }

template <typename I>
constexpr I bitwise_nor(I lhs, I rhs) noexcept { return ~(lhs | rhs); }

and if you want to apply it to a vector of elements, you can use std::transform or std::for_each

einpoklum
  • 102,731
  • 48
  • 279
  • 553