How does the use of other numerical bases in C#work?

Asked

Viewed 105 times

3

When I was learning the basics of variables in C#, I learned that the whole numerical statements (Int32) used the numerical base 10, which is what we use most commonly in our day to day life. A little later, I discovered that I could use hexadecimal to declare variables Int32, adding a 0x before.

With that, my doubts are:

  • How does this behave in the compiler? It converts everything to decimal before turning binary?
  • There are other types of numerical bases that we can use in C# without having to do conversions?
  • 1

    Just out of curiosity: most languages use only base 10 and base 16, some allow base 2 and/or 8 as well. Boolfuck only works on the basis of 2 and Java2k uses numerical basis only 11.

1 answer

4


It’s just a syntax, just so the programmer feels more comfortable typing the number in the way the domain he’s treating usually works.

Most of the time it’s the same decimal. There are some typical problems when dealing with things from the computer that hexadecimal or binary is most suitable. For the application is a number, no matter how it was represented in the code. It’s just convenience, there’s nothing special. This has nothing to do with typing.

There is no such thing as converting everything to decimal, because the decimal itself is a representation. In fact we can say that everything is always converted to binary, which is how the computer understands it. But it’s binary even, it’s bits. What you see there in the code in binary notation is text with the characters 0 and 1, it’s not a binary number, it’s just a textual representation.

C# did not adopt the octal that other languages usually have because it is practically not used anymore in practice and usually generates confusion. There are methods that convert texts to this, or from this base.

Then in Hexadecimal you can represent with the prefix 0x (ex.: 0xD08C) or in binary with prefix 0b (ex.: 0b1001_1101), decimal is the default and need nothing extra.

Note that it is possible to use tab wherever you want in any representation.

You can represent the same number in several ways, now I will represent the number 3:

3 patos de borracha

Browser other questions tagged

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