Skip to content

Fixnum#bit_length always returns 64 for negative number #3829

@jkr2255

Description

@jkr2255

According to CRuby manual, Fixnum#bit_length returns "the bit position of the highest bit which is different to the sign bit". In other words, check 0 bit position for negative numbers.

In JRuby implementation it returns 64 - Long.numberOfLeadingZeros(value).

Long.numberOfLeadingZeros(value) returns 0 if value is negative, so the value is fixed to 64 in the cases.

Environment

  • jruby 9.0.4.0 (2.2.2) 2015-11-12 b9fb7aa OpenJDK 64-Bit Server VM 25.77-b03 on 1.8.0_77-b03 +jit [linux-amd64] on CentOS 7 x64
  • JRuby 9.0.5.0 (x64) on Windows7 x64

Expected Behavior

p (-1).bit_length # => 0

Actual Behavior

p (-1).bit_length # => 64

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions