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

Inconsistency in representation of binary strings #11412

Closed
nkarl opened this issue Dec 24, 2023 · 8 comments
Closed

Inconsistency in representation of binary strings #11412

nkarl opened this issue Dec 24, 2023 · 8 comments
Labels
inconsistent-behavior Behavior between different commands or types inconsistent/unexpected needs-triage An issue that hasn't had any proper look

Comments

@nkarl
Copy link

nkarl commented Dec 24, 2023

Describe the bug

  • Format 1: 001b
  • Format 2: 0b01

Only the second format is functional when used with into bits. In help bits the format used is the first format.

How to reproduce

  1. 010b | into bits
  2. 0b10 | into bits

Expected behavior

  1. 010b | into bits should evaluate to 00000010.
  2. Instead, it evaluates to 00001010.

Screenshots

Untitled design

Configuration

key value
version 0.88.1
branch
commit_hash
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.74.1 (a28077b28 2023-12-04)
rust_channel stable-x86_64-unknown-linux-gnu
cargo_version cargo 1.74.1 (ecb9851af 2023-10-18)
build_time 2023-12-19 19:09:15 -08:00
build_rust_channel release
allocator mimalloc
features dataframe, default, extra, sqlite, trash, which, zip
installed_plugins

Additional context

No response

@nkarl nkarl added the needs-triage An issue that hasn't had any proper look label Dec 24, 2023
@amtoine amtoine added the inconsistent-behavior Behavior between different commands or types inconsistent/unexpected label Dec 24, 2023
@fdncred
Copy link
Collaborator

fdncred commented Dec 24, 2023

I don't follow. 10 bytes is 0000 1010. We don't have any bits representation in nushell. 10b and 10B are both bytes.

@IanManske
Copy link
Member

IanManske commented Dec 24, 2023

The command's first example is probably confusing. It says:

convert a binary value

but then uses a filesize, 01b (1 byte), as input to the command. It should be a binary value: 0x[1].

Also, the integer example should maybe use a binary literal 0b10 (= 2 in base 10) to highlight the difference from a filesize.

@fdncred
Copy link
Collaborator

fdncred commented Dec 24, 2023

those changes, Ian, seem reasonable.

@nkarl
Copy link
Author

nkarl commented Dec 25, 2023

I don't follow. 10 bytes is 0000 1010. We don't have any bits representation in nushell. 10b and 10B are both bytes.

This was the source of my confusion. I assumed that B is for bytes and b is for bits and that is the general consensus. For example, in Vim the unit for characters is B for bytes.

There is also no documentation on various formats of integer at the moment. Traditionally in C we have:

  1. 0b11111111 in binary for 255, b for binary.
  • changed to 0x[11111111]
  • this is confusing.
  1. 0xff in hex for 255, x for hex.

In Rust, the formats also follow the same convention, 0b for bin and 0x for hex.

Because of these assumptions, I thought that Nu allowed an extra format, e.g. 11111111b as binary literal.

@fdncred
Copy link
Collaborator

fdncred commented Dec 25, 2023

we have 0b, 0x, and 0o as literal inputs but i believe the output is always in bytes for literals. If you want to force binary you can use into binary, which then gives a hexadecimal display since binary isn't represented well in a terminal.

@nkarl
Copy link
Author

nkarl commented Dec 25, 2023

That makes sense now. Thanks.

@amtoine
Copy link
Member

amtoine commented Dec 26, 2023

@nkarl
i did not follow the whole discussion, is there still an issue to be addressed? 😇

@nkarl
Copy link
Author

nkarl commented Dec 26, 2023

I was misunderstanding the behavior. It's all clear to me now. Thanks

@nkarl nkarl closed this as completed Dec 26, 2023
fdncred pushed a commit that referenced this issue Apr 26, 2024
# Description
One example for `into bits` says it uses binary value when it actually
uses a filesize. This lead to issue #11412, but I never got around to
fixing the example until this PR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inconsistent-behavior Behavior between different commands or types inconsistent/unexpected needs-triage An issue that hasn't had any proper look
Projects
None yet
Development

No branches or pull requests

4 participants