How to check with binary operations?

Asked

Viewed 104 times

2

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

How can I make one if to know what kind of errors I am dealing with without using regular expressions, only binary operations?

Of the kind:

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

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

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

  • Sorry to be annoying, but: http://meta.stackexchange.com/a/233676/176034

2 answers

3


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 error categories, and 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 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 the creation of 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.

  • That’s exactly what I wanted. I’ll test it, thank you.

2

Well, first than in Java (and also C#, Javascript, C++, Python, among others), using error codes is considered a bad practice of programming. This is why the exception mechanism was invented, so that the error codes become unnecessary being replaced by objects that carry information about the error occurred without having to pollute the domain of the value 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.
     }
 }
  • I wanted something lower level, with binary OR or shifts...

  • @Jorgeb. You’re already an experienced user around here and we’ve met several times. So I ask the question: What exactly do you want to get? Why do you necessarily need to use binary OR and shifts? It seems to me an example of XY problem.

  • Victor, you’re right, but I didn’t know how to ask that if I did, I probably would have found what I want in a poll. I said in the question "only binary operations". What I wanted is something like @Onosendai now replied.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.