#Concise way to specify a negative usize value

1 messages · Page 1 of 1 (latest)

faint palm
#

I need a counterpart of C++'s (size_t)(-1). The best I could come up with so far is

@as(usize, @bitCast(@as(isize, -1)))

Is there a more concise way?

NB. Please don't suggest ideas like inverting all bits of a zero, I need a generic way to specify various negative values.

thorn cloak
#

make a function for it?

dim verge
#

-x is represented as ~@(uN, x) + 1

thorn cloak
dim verge
#

it's the relationship that defines twos complement

thorn cloak
#

would this work?

@as(usize, 0) -% 1
dim verge
#

it should

faint palm
faint palm
dim verge
#

0 -% x is definitely as readable as you're going to get it

thorn cloak
#

yeah I find it readable, but functions are good too

faint palm
#

My issue with readability of the subtraction approach is that it doesn't directly express the idea, but rather makes a (relatively obvious, but nevertheless) equivalent representation of it. I want a negative number, but I have to write a subtraction from zero instead. This is not how I personally define readability.

dim verge
#

on the other hand, I forgot we had unary wrapping

#

it's just -% x

faint palm
#

Do we???

#

Oh, that's pretty nice

#

Then -%@as(usize,1) it is, I guess

dim verge
#

yep, no getting away from giving it a type

faint palm
#

giving it a type is not my issue, the length of the original expression is

thorn cloak
#

I had no idea this was a thing lol

faint palm
#

NB. Please don't suggest ideas like inverting all bits of a zero, I need a generic way to specify various negative values.