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

Trivial documentation issue with core::ops::Bit{And|Or|Xor} #78619

Closed
gabhijit opened this issue Nov 1, 2020 · 0 comments · Fixed by #78620
Closed

Trivial documentation issue with core::ops::Bit{And|Or|Xor} #78619

gabhijit opened this issue Nov 1, 2020 · 0 comments · Fixed by #78620
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-bug Category: This is a bug.

Comments

@gabhijit
Copy link
Contributor

gabhijit commented Nov 1, 2020

This is a documentation issue.

In the documentation for BitOps -> BitOr, BitAnd and BitXor, Following documentation should be changed to use Bitwise operators for Boolean operations and not the logical operations.

// For BitOr

use std::ops::BitOr;

#[derive(Debug, PartialEq)]
struct BooleanVector(Vec<bool>);

impl BitOr for BooleanVector {
    type Output = Self;

    fn bitor(self, Self(rhs): Self) -> Self::Output {
        let Self(lhs) = self;
        assert_eq!(lhs.len(), rhs.len());
        Self(lhs.iter().zip(rhs.iter()).map(|(x, y)| *x || *y).collect()) // <- Here we should use bit-wise or and not logical or 
    }
}

let bv1 = BooleanVector(vec![true, true, false, false]);
let bv2 = BooleanVector(vec![true, false, true, false]);
let expected = BooleanVector(vec![true, true, true, false]);
assert_eq!(bv1 | bv2, expected);

Also, similar changes are required for Bitwise And and Bitwise Xor. Note: While the output is not wrong per se, it's actually a bit confusing since, those operators already exist for scalar versions of bool.

The changes for BitXor are more confusing

// <snip>
impl BitXor for BooleanVector {
    type Output = Self;

    fn bitxor(self, Self(rhs): Self) -> Self::Output {
        let Self(lhs) = self;
        assert_eq!(lhs.len(), rhs.len());
        Self(lhs.iter()
                .zip(rhs.iter())
                .map(|(x, y)| (*x || *y) && !(*x && *y))
                .collect())
    }
}

let bv1 = BooleanVector(vec![true, true, false, false]);
let bv2 = BooleanVector(vec![true, false, true, false]);
let expected = BooleanVector(vec![false, true, true, false]);
assert_eq!(bv1 ^ bv2, expected);

Where a simple bitwise xor above like .map(|(x, y)| (*x ^ *y)) should suffice.

I have quickly verified these changes indeed work on playground using nightly and stable.

Meta

Since this is really a documentation issue, added for completeness.

rustc --version --verbose:

$ rustc --version --verbose
rustc 1.47.0 (18bf6b4f0 2020-10-07)
binary: rustc
commit-hash: 18bf6b4f01a6feaf7259ba7cdae58031af1b7b39
commit-date: 2020-10-07
host: x86_64-unknown-linux-gnu
release: 1.47.0
LLVM version: 11.0
@gabhijit gabhijit added the C-bug Category: This is a bug. label Nov 1, 2020
@gabhijit gabhijit changed the title Trivial documentation issue with core::ops::Bit{And|Or|Xor| Trivial documentation issue with core::ops::Bit{And|Or|Xor} Nov 1, 2020
gabhijit added a commit to gabhijit/rust that referenced this issue Nov 1, 2020
Added fixes to documentation of `BitAnd`, `BitOr`, `BitXor` and
`BitAndAssign`, where the documentation for implementation on
`Vector<bool>` was using logical operators in place of the bitwise
operators.

r? @steveklabnik
cc rust-lang#78619
m-ou-se added a commit to m-ou-se/rust that referenced this issue Nov 1, 2020
Trivial fixes to bitwise operator documentation

Added fixes to documentation of `BitAnd`, `BitOr`, `BitXor` and
`BitAndAssign`, where the documentation for implementation on
`Vector<bool>` was using logical operators in place of the bitwise
operators.

r? @steveklabnik
cc rust-lang#78619
@jyn514 jyn514 added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Nov 1, 2020
@bors bors closed this as completed in 7baf48f Nov 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants