Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is typeof hex or binary number Uint64 while type of decimal number is Int64?

Tags:

julia

julia> typeof(-0b111)
Uint64

julia> typeof(-0x7)
Uint64

julia> typeof(-7)
Int64

I find this result a bit surprising. Why does the numeric base of the number determine signed or unsgined-ness?

like image 632
aneccodeal Avatar asked Dec 08 '14 00:12

aneccodeal


2 Answers

Looks like this is expected behavior:

This behavior is based on the observation that when one uses unsigned hex literals for integer values, one typically is using them to represent a fixed numeric byte sequence, rather than just an integer value.

http://docs.julialang.org/en/latest/manual/integers-and-floating-point-numbers/#integers

...seems like a bit of an odd choice.

like image 198
aneccodeal Avatar answered Sep 28 '22 01:09

aneccodeal


This is a subjective call, but I think it's worked out pretty well. In my experience when you use hex or binary, you're interested in a specific pattern of bits – and you generally want it to be unsigned. When you're just interested a numeric value you use decimal because that's what we're most familiar with. In addition, when you're using hex or binary, the number of digits you use for input is typically significant, whereas in decimal, it isn't. So that's how literals work in Julia: decimal gives you a signed integer of a type that the value fits in, while hex and binary give you an unsigned value whose storage size is determined by the number of digits.

like image 25
StefanKarpinski Avatar answered Sep 28 '22 01:09

StefanKarpinski