How to do verification with binary operations?

I have an application where I separate the errors by range , e.g. system errors of 10-19, i.e. 10 slots for system errors, then I have login errors of 20-29, etc.

How can I do a if to know what kind of errors I'm dealing with without using regular expressions, only binary operations?

Of the genus:

if( erro ==  20|21|22|23 )
{
     //faz qualquer coisa
}

That is, if it is a login error do anything. If for example erro = 21 then enter the if.

I'm doing this in Java, but I think the idea might suit any situation.

Author: Maniero, 2016-06-23

2 answers

If you want to target in a binary way, one possibility would be to use bit flags in a practice called bit masking.

The operation is quite simple. Let's say you have 8 categories of errors, and you want to reserve up to 256 possible errors in each category. You can then use a short integer (16 bits) to store all possible errors:

Categoria         Erro
0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0

Let's say the category of bit 0 correspond to login, and 1 to system:

Categoria         Erro              Dec   Descrição
0 0 0 0 0 0 0 1   0 0 0 0 0 0 0 1   257   Login: Usuário não encontrado
0 0 0 0 0 0 0 1   0 0 0 0 0 0 1 0   258   Login: Senha incorreta
0 0 0 0 0 0 1 0   0 0 0 0 0 0 0 1   513   Sistema: Falha na inicialização
0 0 0 0 0 0 1 0   0 0 0 0 0 0 1 0   514   Sistema: Erro de configucação

Finally, use and operations to determine if the error is of a certain category:

Se erro AND 256 = Tipo Login
Se erro AND 512 = Tipo Sistema

One of the advantages of this method is that it allows you to create elements that fit into 2 or more categories:

0 0 0 0 0 0 1 1   0 0 0 0 0 0 1 1   771   Sistema/Login: Provedor Oauth não definido

Finally, a simple Javascript implementation:

var cats = {
  "login": Math.pow(2, 8), // Bit 9
  "sistema": Math.pow(2, 9) // Bit 10
  };

console.log(!!(257 & cats.login));   // erro 257 é tipo login,
console.log(!!(257 & cats.sistema)); // porém não tipo sistema.
 3
Author: OnoSendai, 2016-06-23 16:57:50

Well, first of all in Java (and also C#, Javascript, C++, Python, among others), using error codes is considered a bad programming practice. This is why the exception mechanism was invented, so that error codes became unnecessary by being replaced with objects that carry information about the error that occurred without having to pollute the value domain of the return of functions.

However, assuming that for some reason you cannot simply take these error codes and replace them with exceptions, then:

 private static final int ERRO_DE_SISTEMA_MIN = 10;
 private static final int ERRO_DE_SISTEMA_MAX = 19;
 private static final int ERRO_DE_LOGIN_MIN = 20;
 private static final int ERRO_DE_LOGIN_MAX = 29;

 public static boolean isErroSistema(int codigo) {
     return codigo >= ERRO_DE_SISTEMA_MIN && codigo <= ERRO_DE_SISTEMA_MAX;
 }

 public static boolean isErroLogin(int codigo) {
     return codigo >= ERRO_DE_LOGIN_MIN && codigo <= ERRO_DE_LOGIN_MAX;
 }

 public void minhaOperacao() {
     codigo = ...;
     if (isErroSistema(codigo)) {
         // Trata o erro de sistema.
     } else if (isErroLogin(codigo)) {
         // Trata o erro de login.
     }
 }
 2
Author: Victor Stafusa, 2016-06-23 16:14:05