How to check with binary operations?

2

I have an application in which I separate errors by range , for example, system errors of 10-19, ie 10 slots for system errors, then I have 20- 29, etc.

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

From the genre:

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

That is, if it is a login error it does anything. If for example erro = 21 then enters if .

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

    
asked by anonymous 23.06.2016 / 18:03

2 answers

3

If you want to target binary, 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 error categories, 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 that the category of bit 0 corresponds 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 two 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 implementation in Javascript:

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.
    
23.06.2016 / 18:18
2

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

However, assuming that for some reason you can not simply remove 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.
     }
 }
    
23.06.2016 / 18:14