Skip to main content
48 votes
Accepted

Should bytes be signed?

Frame Challenge: Bytes should not be integers. Bytes are bytes, a fixed-size sequence of bits, typically 8 on modern computers. Should an array of bits be signed or unsigned? Neither, the question is ...
Matthieu M.'s user avatar
  • 3,038
30 votes

What are the drawbacks of allowing implicit boolean/integer conversions?

The main drawback of allowing implicit boolean/integer conversions is, it makes it impossible to prevent errors that result from confusion of a boolean and an integer. Next time you confuse the order ...
Eldritch Conundrum's user avatar
29 votes
Accepted

What are some advantages/disadvantages of having built-in syntax to represent numbers in non-decimal bases?

One advantage is bitwise arithmetic. Bitwise arithmetic is much easier to understand in bases with a power of 2 radix such as hexadecimal. Here it is fairly clear that I am masking out every other ...
CPlus's user avatar
  • 8,321
24 votes
Accepted

What are the drawbacks of allowing implicit boolean/integer conversions?

There is a particular drawback of allowing implicit conversions (aka coercions*) from bool to int in dynamic languages such as ...
kaya3's user avatar
  • 19.7k
20 votes

Should bytes be signed?

Bytes should be unsigned There are a few reasons most languages have unsigned bytes by default: A more useful range. Bytes can only represent 256 different numbers so using them effectively is extra ...
mousetail's user avatar
  • 8,359
19 votes

What are some advantages/disadvantages of having built-in syntax to represent numbers in non-decimal bases?

In a domain where a specific base is used, it is clearer if the code can reflect this and still be efficient. For example, airplane transponders use base 8, and colours are often represented in base ...
Adám's user avatar
  • 3,156
17 votes
Accepted

Why are expressions in languages so prone to integer overflow?

Overflow checking has a (small) cost The hardware will always overflow. It doesn't care. In order to check if an overflow happened, you need extra instructions (typically just a single ...
Rydwolf Programs's user avatar
13 votes

Invalid signed integer in C and similar languages

C is about what the machine may have (even now) A little history, before the answer. C is a direct successor of B, and B has really one type: the machine register, with then could act as a machine ...
André L F S Bacci's user avatar
12 votes

Why don't many languages have integer range types?

The most popular reason is simply that having additional types is more work for the authors of compilers and libraries. Every feature starts with -100 points, and only gets implemented when there's a ...
dan04's user avatar
  • 1,959
11 votes

What are the advantages of having a set number of fixed sized integers versus defining the exact number of bits in every integer?

You need to distinguish between two meanings of “5-bit integer”: An integer which occupies 5 bits of storage, so that three of them will fit into a 16-bit word. An integer which can hold values ...
Martin Kochanski's user avatar
10 votes

Should bytes be signed?

Signed bytes make bit-packing difficult, because when a signed byte is converted to a wider integer type, sign extension means that the wider integer might use more than just the low 8 bits. Consider ...
kaya3's user avatar
  • 19.7k
9 votes
Accepted

Why short and long and double and similar instead of unambigous width types such as int16 or int64 or float64?

Believe it or not, the original idea in C was that you weren't supposed to worry about the size, and other languages made shortly thereafter followed suit. The idea was "use ...
Silvio Mayolo's user avatar
9 votes

What are some advantages/disadvantages of having built-in syntax to represent numbers in non-decimal bases?

The other answers so far have exclusively focused on advantages. There are two disadvantages I can think of, though neither is really a strong enough reason to exclude this feature from a language. ...
kaya3's user avatar
  • 19.7k
9 votes

What, if any, runtime cost is incurred by using 64-bit ints on 32-bit platforms in LLVM?

On a 32-bit platform 64-bit integers will be more than twice as expensive as 32-bit ones. First there are the operations to consider. Simple addition and subtraction will require two 32-bit operation ...
Peter Green's user avatar
9 votes

What are the drawbacks of allowing implicit boolean/integer conversions?

Booleans may not just be 1 or 0 Much of this depends on the underlying representation of the value. My particular experience of this is with C. Before C99, there was no boolean type, only integers ...
Graham's user avatar
  • 281
8 votes

Why don't many languages have integer range types?

In addition to the other fine answers, I'll note that many languages outsource their underlying type system to a "lowest common denominator" type system provided by a runtime, and then layer ...
Eric Lippert's user avatar
  • 3,459
8 votes

What are the drawbacks of allowing implicit boolean/integer conversions?

In addition to all the other fine answers, Boolean coercions are not consistent across languages: Zero (0) is considered falsy in Python and JavaScript, but truthy ...
Nayuki's user avatar
  • 181
8 votes

Should bytes be signed?

This still boils down to "what do you intend to do with them?" As others have noted, sign extension is a nuisance when deserializing a wider integer, which suggests that unsigned is more ...
Martin Kealey's user avatar
7 votes

What are some advantages/disadvantages of having built-in syntax to represent numbers in non-decimal bases?

Runtime safety With syntax like 0x1A50 instead of parseInt("1A50",16) you can make sure at compile time that ...
kouta-kun's user avatar
  • 1,359
7 votes

Why are expressions in languages so prone to integer overflow?

While overflow-trapped arithmetic may impose only a moderate cost to a non-optimizing compiler, most languages that support overflow trapping treat any signals, traps, exceptions, etc. as strongly ...
supercat's user avatar
  • 1,812
7 votes

Why are expressions in languages so prone to integer overflow?

In addition to @Radvylf Programs' answer, some compilers for languages like C/C++ which abuse undefined behaviour can make use of assuming that overflow simply does not exist, and eliminate branches ...
kouta-kun's user avatar
  • 1,359
7 votes

Invalid signed integer in C and similar languages

Making equal numbers of positive and negative numbers is not necessarily a pleasing mathematical property. You get rid of the case that "INT_MIN is its own negation" but at a cost. Two's ...
qwr's user avatar
  • 259
7 votes

Invalid signed integer in C and similar languages

C was designed as an abstraction which covered features of hardware existing at the time. Since there were no mainstream CPUs that would produce a special NaN value ...
Dmitry Grigoryev's user avatar
6 votes

Invalid signed integer in C and similar languages

Use the R language R has NA, which can be an integer and has the semantics of NaN. R's NA can also be a boolean (logical) and it'...
tarzh's user avatar
  • 3,924
6 votes

Why are expressions in languages so prone to integer overflow?

Aside from the runtime cost, representing the overflow in integer computations has an added complexity cost for the programmer. Unlike floats, where there is a NaN available to signal that something ...
Nuoji's user avatar
  • 1,105
6 votes

Why short and long and double and similar instead of unambigous width types such as int16 or int64 or float64?

C's choice to make integer widths depend on the hardware (or, strictly speaking, the compiler's choice ─ but in practice, compilers choose based on the hardware) means these types are "leaky ...
kaya3's user avatar
  • 19.7k
6 votes

What are the drawbacks of allowing implicit boolean/integer conversions?

One particularly bad issue with int to bool conversions is that if you have an expression based language, the assignment x=7 will typically return ...
Oscar Smith's user avatar
5 votes

What are the drawbacks of allowing implicit boolean/integer conversions?

There are some common errors that result from integer to boolean conversions. One of the most common among C beginners is the strcmp() function. As a comparison ...
Barmar's user avatar
  • 1,901
5 votes
Accepted

Correctness of mixed signed/unsigned arithmetic

So is there a correct way to implement this? Any pitfalls and footguns I should be considering? How do other languages do it? Your proposed struct value is utterly bizarre and no language I know of ...
Moonchild's user avatar
  • 879
5 votes

Correctness of mixed signed/unsigned arithmetic

This code causes the C compiler to emit warnings like: [...] Which makes me worried about the correctness of this implementation. Let's examine this with an example: ...
André L F S Bacci's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible