A 3 level comparator - compare

I want to make a comparator with some tolerance.
I have taken the difference between the two signals (hopefully)
Now I want to compare to a number (which will be decided later) and respectively forward signals of equal notequal and acceptable.
I have let the commented portion to help you understand my approach.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.numeric_std.all;
use ieee.std_logic_unsigned.all;
entity threelvlcomp is
port (
input1 : in std_logic_vector(15 downto 0);
input2 : in std_logic_vector(15 downto 0);
outputeq : out std_logic;
outputneq : out std_logic
);
end threelvlcomp;
architecture Behavioral of threelvlcomp is
signal temp : std_logic_vector(15 downto 0);
signal dif : std_logic_vector(15 downto 0);
--CONSTANT limit : INTEGER := 1;
begin
dif <= std_logic_vector(unsigned(input1)) + std_logic_vector(unsigned(not(input2) + 1));
--dif <= input1 + not(input2)+ "1";
--dif <= input1 + not(input2) + "1";
outputeq <= '1' when dif < '1' else
'0';
outputneq <= '1' when dif > '1' else
'0';
-- IF dif = "0000" THEN
-- outputeq <= '1';
-- outputneq <= '0';
-- outputacc <= '0';
--ELSE IF dif /= '0' THEN
-- outputeq <= '0';
-- outputneq <= '1';
--outputacc <= '0';
--ELSE IF dif <= "0.5";
-- outputeq <= '0';
-- outputneq <= '0';
-- outputacc <= '1';
--ELSE
-- outputeq <= '1';
-- outputneq <= '1';
-- outputacc <= '1';
--END IF;
-- If(dif = 0) then outputeq <='1'; else outputeq <= '0';end if;
-- If(dif >= -0.5 and dif <= 0.5) then outputacc <='1'; else outputacc <= '0';end if;
-- If(dif /= 0) then outputneq <='1' else outputneq <= '0';end if;
end Behavioral;

I have not tested this code. But to me looks like it is what you are trying to do. It should at least help you with your signed/unsigned issue.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.numeric_std.all;
--use ieee.std_logic_unsigned.all; --do not use this it is confusing you
entity threelvlcomp is
port (
input1 : in std_logic_vector(15 downto 0);
input2 : in std_logic_vector(15 downto 0);
outputeq : out std_logic;
outputneq : out std_logic
);
end threelvlcomp;
architecture Behavioral of threelvlcomp is
signal temp : unsigned(15 downto 0);
signal dif : signed(16 downto 0);
--CONSTANT limit : INTEGER := 1;
begin
-- you want a signed number here so we need to signed extend with a zero
dif <= (signed('0'input1)) + (not signed('0'&input2)) + 1;
--dif <= input1 + not(input2)+ "1";
--dif <= input1 + not(input2) + "1";
outputeq <= '1' when dif < 1 else
'0';
outputneq <= '1' when dif > 1 else
'0';
end Behavioral;

Related

Why is "end if" missing at the end of an if-else sequence?

The error is
teclat.vhdl:57:12: 'if' is expected instead of 'process'
The end if is where it is supposed to be. I don't understand why it doesn't compile like it's missing an end if always.
architecture v1 of teclat is
begin
process(Buttons, COL, ROW) begin
if (Buttons(1) = '1') then
COL <= "100";
ROW <= "100";
else if (Buttons(2) = '1') then
COL <= "010";
ROW <= "100";
else if (Buttons(3) = '1') then
COL <= "001";
ROW <= "100";
else
ROW <= "111";
end if;
end process;
end v1;
There is one end if where it is supposed to be, but you are missing the ones for the remaining if statements you opened.
Your code is currently processed as
if (Buttons(1) = '1') then
COL <= "100";
ROW <= "100";
else
if (Buttons(2) = '1') then
COL <= "010";
ROW <= "100";
else
if (Buttons(3) = '1') then
COL <= "001";
ROW <= "100";
else
ROW <= "111";
end if;
end if; -- missing
end if; -- missing
Alternatively, use elsif to avoid opening so many if statements. Now you have only a single if statement and need only one end if.
if (Buttons(1) = '1') then
COL <= "100";
ROW <= "100";
elsif (Buttons(2) = '1') then
COL <= "010";
ROW <= "100";
elsif (Buttons(3) = '1') then
COL <= "001";
ROW <= "100";
else
ROW <= "111";
end if;

Affine cipher decryption, output differs for upper case and lower case

I have the problem when decrypting a plaintext using Affine cipher.
Encryption works fine, but applying the same logic for decryption of lower case/upper case characters returns different output.
Here is the output:
Encrypted Message is : ulctkbsjarizqhypgxofwnevmd ULCTKBSJARIZQHYPGXOFWNEVMD
Decrypted Message is: opqrstuvwxyzabcdefghijklmn ABCDEFGHIJKLMNOPQRSTUVWXYZ
I suspect it has something to do with retrieving of ASCII values, can someone correct me?
Here is my code:
#include<bits/stdc++.h>
using namespace std;
//Key values of a and b
const int a = 17;
const int b = 20;
string encryptMessage(string plainText)
{
string cipher = "";
for (int i = 0; i < plainText.length(); i++)
{
if(plainText[i]!=' ')
{
if ((plainText[i] >= 'a' && plainText[i] <= 'z') || (plainText[i] >= 'A' && plainText[i] <= 'Z'))
{
if (plainText[i] >= 'a' && plainText[i] <= 'z')
{
cipher = cipher + (char) ((((a * (plainText[i]-'a') ) + b) % 26) + 'a');
}
else if (plainText[i] >= 'A' && plainText[i] <= 'Z')
{
cipher = cipher + (char) ((((a * (plainText[i]-'A') ) + b) % 26) + 'A');
}
}
else
{
cipher += plainText[i];
}
}
else
{
cipher += plainText[i];
}
}
return cipher;
}
string decryptCipher(string cipher)
{
string plainText = "";
int aInverse = 0;
int flag = 0;
for (int i = 0; i < 26; i++)
{
flag = (a * i) % 26;
if (flag == 1)
{
aInverse = i;
}
}
for (int i = 0; i < cipher.length(); i++)
{
if(cipher[i] != ' ')
{
if ((cipher[i] >= 'a' && cipher[i] <= 'z') || (cipher[i] >= 'A' && cipher[i] <= 'Z'))
{
if (cipher[i] >= 'a' && cipher[i] <= 'z')
{
plainText = plainText + (char) ((((aInverse * (cipher[i]+ 'a') ) - b) % 26) + 'a');
}
else if (cipher[i] >= 'A' && cipher[i] <= 'Z')
{
plainText = plainText + (char) (((aInverse * ((cipher[i]+'A' - b)) % 26)) + 'A');
}
}
else
{
plainText += cipher[i];
}
}
else
plainText += cipher[i];
}
return plainText;
}
//Driver Program
int main(void)
{
string msg = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//Calling encryption function
string cipherText = encryptMessage(msg);
cout << "Encrypted Message is : " << cipherText<<endl;
//Calling Decryption function
cout << "Decrypted Message is: " << decryptCipher(cipherText);
return 0;
}
I have been thinking about this for some time and, although I can't provide a complete explanation, I have a couple of 'observations' that may be useful, plus a 'cheat' workaround.
First, although you say you use "the same logic for decryption of lower case/upper case," a character-wise alignment of the code from each of your decryption blocks shows that this isn't quite true:
plainText = plainText + (char) ((((aInverse * (cipher[i]+ 'a') ) - b) % 26) + 'a'); // Lower case
plainText = plainText + (char) (((aInverse * ((cipher[i]+'A' - b)) % 26)) + 'A'); // Upper case
So, 'fixing' the lower case code to be exactly analogous to the (working) code for upper case (and removing redundant parentheses) gives this:
if (cipher[i] >= 'a' && cipher[i] <= 'z')
{
plainText = plainText + (char)( ( (aInverse * (cipher[i] + 'a' - b) ) % 26 ) + 'a' );
}
else if (cipher[i] >= 'A' && cipher[i] <= 'Z')
{
plainText = plainText + (char)( ( (aInverse * (cipher[i] + 'A' - b) ) % 26 ) + 'A' );
}
However, this doesn't actually resolve the issue (it just changes it slightly), as the output then is as follows:
Encrypted Message is : ulctkbsjarizqhypgxofwnevmd ULCTKBSJARIZQHYPGXOFWNEVMD
Decrypted Message is : qrstuvwxyzabcdefghijklmnop ABCDEFGHIJKLMNOPQRSTUVWXYZ
The problem here is that the lowercase values are all 'rotated' by the value 16 - which looks suspiciously close to the value for a. Also, note that, although a is used in the encryption formula, it is not used in your decryption.
So, I have come up with the following workaround, assuming that (for reasons yet to be deduced) when decoding upper case values, this 16 is somehow lost in the bit-representation of the ASCII values:
if ((cipher[i] >= 'a' && cipher[i] <= 'z') || (cipher[i] >= 'A' && cipher[i] <= 'Z'))
{
int offset = ((cipher[i] - 'A') / 26) ? a - 1 : 0;
if (cipher[i] >= 'a' && cipher[i] <= 'z') {
plainText = plainText + (char)( ( (aInverse * (cipher[i] + 'a' - b) - offset ) % 26 ) + 'a' );
}
else if (cipher[i] >= 'A' && cipher[i] <= 'Z') {
plainText = plainText + (char)( ( (aInverse * (cipher[i] + 'A' - b) - offset ) % 26 ) + 'A' );
}
}
Note that your code can be further simplified/clarified using functions provided by the standard library and removing some 'redundant' checks:
for (int i = 0; i < cipher.length(); i++) {
if (isalpha(cipher[i])) {
int offset = islower(cipher[i]) ? a - 1 : 0;
int letter = islower(cipher[i]) ? 'a' : 'A';
plainText = plainText + (char)(((aInverse * (cipher[i] + letter - b) - offset) % 26) + letter);
}
else {
plainText += cipher[i];
}
}

Double 'else' statement in pascal

I was trying to translate the following Pascal code to C++, when I stumbled upon the "else else" construction in question. I've never seen this before, so could anybody tell me what it does and what are it's C++ (or maybe C) equivalents?
Procedure Force(Q:Int64;V,K:Integer);
Var i,j,t:Integer;
begin
if K<=0 then
if (Q>=A)and(Q Mod KK =0)and(V>=S)and(V<=F)then Out:=Out+1 else else
For i:=0 to 9 do
if (Q+(i+1)*h[k-1]>=A)and(Q+i*h[k-1]<=B) then
if (Q+(i+1)*h[K-1]<B)and(Q+i*h[k-1]>=A) then
Begin
M:=(Q+i*h[k-1]) Mod KK;
For j:=0 to 9*(K-1) do
For t:=0 to KK-1 do
if D[K-1,j,t]>0 then
if (V+i+j>=S)and(V+i+j<=F)and((t+M) Mod KK=0) then
Out:=Out+D[K-1,j,t];
end else
if Odd(N-K+1) then Force(Q+i*h[k-1],V+i,K-1) else
Force(Q+i*h[k-1],V+i,K-1);
end;
I've just copied to an editor (for instance Komodo, where you can select Pascal as language for syntax color highlighting) and reformatted the text you've wrote in a way I can read it myself.
procedure Force(Q:Int64;V,K:Integer);
var
i,j,t:Integer;
begin
if K<=0 then
if (Q>=A) and (Q Mod KK =0) and (V>=S) and (V<=F) then
Out:=Out+1
else
else
for i:=0 to 9 do begin
if (Q+(i+1)*h[k-1]>=A) and (Q+i*h[k-1] <= B) then
if (Q+(i+1)*h[K-1]<B) and (Q+i*h[k-1] >= A) then begin
M := (Q+i*h[k-1]) Mod KK;
for j:=0 to 9*(K-1) do begin
for t:=0 to KK-1 do begin
if D[K-1,j,t] > 0 then
if (V+i+j >= S) and (V+i+j <= F) and ((t+M) mod KK = 0) then
Out:=Out+D[K-1,j,t];
end; {for t}
end; {for j}
end else
if Odd(N-K+1) then
Force(Q+i*h[k-1],V+i,K-1)
else
Force(Q+i*h[k-1],V+i,K-1);
end;
end;
end;
Don't you think it is more understandable now?
It's quite often useful to use begin and end pairs even when they're not required by the syntax, just to make the code more readable and understandable. (Think of begin being the equivalent of { and end being the equivalent of }; while you can write for(int i = 0; i < 10; i++) SomeCode();, it's usually more clear to use for(int i = 0; i < 10; i++) { SomeCode(); }.
So the code you've posted, with begin and end pairs added where appropriate, a no-op else or two removed, and more appropriate formatting seems much more readable to me.
Procedure Force(Q: Int64; V, K: Integer);
Var
i, j, t: Integer;
begin
if K <= 0 then
begin
if (Q >= A) and (Q Mod KK = 0) and (V >= S) and (V <= F) then
Out := Out + 1;
end
else
begin
For i := 0 to 9 do
begin
if (Q + (i + 1) * h[K - 1] >= A) and (Q + i * h[K - 1] <= B) then
begin
if (Q + (i + 1) * h[K - 1] < B) and (Q + i * h[K - 1] >= A) then
begin
M := (Q + i * h[K - 1]) Mod KK;
For j := 0 to 9 * (K - 1) do
begin
For t := 0 to KK - 1 do
begin
if D[K - 1, j, t] > 0 then
begin
if (V + i + j >= S) and (V + i + j <= F) and
((t + M) Mod KK = 0) then
Out := Out + D[K - 1, j, t];
end;
end;
end;
end
else if Odd(N - K + 1) then
Force(Q + i * h[K - 1], V + i, K - 1)
else
Force(Q + i * h[K - 1], V + i, K - 1);
end;
end;
end;
end;
That is some horrible indentation. If we indent it better we can see what's going on:
if K<=0 then
if (Q>=A)and(Q Mod KK =0)and(V>=S)and(V<=F) then
Out:=Out+1
else
else
For i:=0 to 9 do
Here, the first else applies to the if (Q>=A), but it's empty.

Wrapping chars in caesar cipher encode

Can anyone please explain me how this wrapping of chars between a-to-z and A-to-Z happening in Caesar shift code?
k %= 26;
for(int i = 0; i < n; i++){
int c = s[i];
if(c >= 'a' && c <= 'z'){
c += k;
if( c > 'z'){
c = 96 + (c % 122); // wrapping from z to a?
}
}
else if(c >= 'A' && c <= 'Z'){
c += k;
if(c > 'Z'){
c = 64 + (c % 90);
}
}
cout << (char)c;
}
K is amount of shift and c is a char of string s.
Is there any better way to do the same?
Lets make a couple changes to the code and it is easier to see what is going on
for(int i = 0; i < n; i++){
int c = s[i];
if(c >= 'a' && c <= 'z'){
c += k;
if( c > 'z'){
c = 'a' + (c % 'z') - 1; // wrapping from z to a?
}
}
else if(c >= 'A' && c <= 'Z'){
c += k;
if(c > 'Z'){
c = 'A' + (c % 'Z') - 1;
}
}
cout << (char)c;
}
So in c = 'a' + (c % 'z') - 1; if c is larger than z then we mod c by z(122) to get how many characters from a we need to go. The same thing is going on with the upper case letters. I am subtracting one here as we are starting at a instead of the character before a like you original code does.

Simplifying a function based on matching the pattern of a string

Question: I'm new to C++ and after writing the following code seems like there should be a way to shorten it. Maybe by somehow matching the string? How would this be done?
The function takes a string message received via Serial port and sets the value of a particular element of the pinValues[] array depending on the message. The value that will be set is determined by the last character H or L just before the \n.
String pattern: (a number)(H or L)\n
Eg: message == "4H\n" will set the 5th element pinValues[4] to HIGH. The number at the start of the string can be 1 to 2 digits.
void setPinValues(String message) {
if( message == "1H\n" ) {
pinValues[1] = HIGH;
}
if( message == "1L\n" ) {
pinValues[1] = LOW;
}
if( message == "2H\n" ) {
pinValues[2] = HIGH;
}
if( message == "2L\n" ) {
pinValues[2] = LOW;
}
if( message == "3H\n" ) {
pinValues[3] = HIGH;
}
if( message == "3L\n" ) {
pinValues[3] = LOW;
}
if( message == "4H\n" ) {
pinValues[4] = HIGH;
}
if( message == "4L\n" ) {
pinValues[4] = LOW;
}
if( message == "5H\n" ) {
pinValues[5] = HIGH;
}
if( message == "5L\n" ) {
pinValues[5] = LOW;
}
if( message == "6H\n" ) {
pinValues[6] = HIGH;
}
if( message == "6L\n" ) {
pinValues[6] = LOW;
}
}
This is probably not the official "C++"-approved way of doing it, but you could do:
unsigned int pinNo = 0;
unsigned char level = 0;
int result = sscanf(message.c_str(), "%u%c", &pinNo, &level);
if (result < 2)
// it failed
if (pinNo > 6)
// bad data
levelVal = (level == 'H') ? HIGH : LOW;
I'd do some sanity checking on the string while extracting the key and value from the first two chars. If you don't need to sanity check the message, it could be as short as
void setPinValues(String message) {
pinValues[ message[0] - '0' ] = (message[1] == 'H') ? HIGH:LOW;
}
Although you may want to make that a little longer, i.e. check the string length, and that the 2 chars your checking are in the right range. i.e
void setPinValues(string message) {
if (
message.size() >= 2
and
message[0] >= '1' and message[0] <= '6'
and (message[1]=='H' or message[1]=='L')
) {
pinValues[ message[0] - '0' ] = (message[1] == 'H') ? HIGH:LOW;
}
}
EDIT: you could also extend that to checking two leading digits, i.e.
int n, off=0;
if ( s[off] <= '9' and s[off] >= '0')
{
n = s[off++] - '0';
}
if ( s[off] <= '9' and s[off] >= '0')
{
n = 10*n + s[off++] - '0';
}
if (off > 0 and (s[1]=='H' or s[1]=='L')) {
pinValues[ message[0] - '0' ] = (message[1] == 'H') ? HIGH:LOW;
}
Assuming String is actually a std::string or has an identical interface, and also assuming an ASCII-compatible character set...
void setPinValues(String message) {
const size_t sz = message.size();
// input validation, ignore the message if it doesn't fit the pattern
// you can remove this "if" block if the message has already been validated
if ( (sz < 3) || (sz > 4)
// note how message[0] will be checked twice if sz == 3
// once as message[0] and once as message[sz -3]
// but if sz == 4 we check message[0] and message[1]
|| (message[0] < '0') || (message[0] > '9')
|| (message[sz - 3] < '0') || (message[sz - 3] > '9')
|| ((message[sz - 2] != 'H') && (message[sz - 2] != 'L'))
|| (message[sz - 1] != '\n'))
return;
// convert the first or two characters to a number
int pinNumber = message[0] - '0';
if (sz == 4)
pinNumber = (pinNumber * 10) + (message[1] - '0');
// additional check to verify the pin number is in the correct range
if ((pinNumber < 1) || (pinNumber > 6))
return;
// apply
pinValues[pinNumber] = (message[sz - 2] == 'H' ? HIGH : LOW);
}