Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitwise expression evaluation? #7281

Closed
halostatue opened this issue Aug 24, 2020 · 3 comments
Closed

Bitwise expression evaluation? #7281

halostatue opened this issue Aug 24, 2020 · 3 comments

Comments

@halostatue
Copy link
Contributor

halostatue commented Aug 24, 2020

I just had reason to use bitwise math expressions in a bash script, and realized that it could be useful to have in Fish, as well. Unfortunately, codeplea/tinyexpr#22 (comment) indicates that, absent the use of a fork (BobBurns/tinyexpr), tinyexpr will not support bitwise expressions (the reasoning for this is good).

I’m wondering if there’s a way to add these. For the specific use case I have (conditional command execution), it is possible to use contains, but there are other cases where it might be useful to have bitwise operations.

@zanchey
Copy link
Member

zanchey commented Aug 25, 2020

It could be added to math, although the fact that it's not in bc, dc or expr does make me wonder how useful it is.

@zanchey zanchey added this to the fish-future milestone Aug 25, 2020
@faho
Copy link
Member

faho commented Aug 25, 2020

So I tested an implementation of this, and it's reasonably simple with the restriction that it just truncates to int, which I don't think would be that awful.

However, I don't think we should use the normal infix operators for this. For one ^ (which would signify xor) is already taken, and & and | just really look like logical operators (but I consider them a mistake in C to begin with).

So I'd lean more towards functions:

math "and(5,3)"

or possibly "bit_and" or something?

@faho faho closed this as completed in 5b1c000 Aug 26, 2020
@faho
Copy link
Member

faho commented Aug 26, 2020

Yeah I ended up implementing these as bitand, bitor and bitxor. They're simply functions that take two arguments, and they truncate to integer before doing their thing, because bitwise operations on floats don't really make sense.

@zanchey zanchey modified the milestones: fish-future, fish 3.2.0 Aug 28, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants