[CSHARP-4771] IsPowerOf2 always return true Created: 24/Aug/23  Updated: 27/Oct/23  Resolved: 31/Aug/23

Status: Closed
Project: C# Driver
Component/s: BSON
Affects Version/s: 2.21.0
Fix Version/s: None

Type: Bug Priority: Unknown
Reporter: Melvin Courjaud Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: bug
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

The function IsPowerOf2 from the PowerOf2.cs file in Bson project always return true :

  • 1 & 0 = 0
  • 0 & 1 = 0

Simple Fix:

```cs

# Replace

var result = (n & (n - 1)) == 0;

# By

var result = n % 2 == 0;

```



 Comments   
Comment by James Kovacs [ 31/Aug/23 ]

Thank you for getting back to us. I will close as Works As Designed.

Comment by Melvin Courjaud [ 31/Aug/23 ]

I apologize, I've mistested the function.

It works correctly.

Comment by Melvin Courjaud [ 24/Aug/23 ]

Sorry for the dead link in description, see https://github.com/mongodb/mongo-csharp-driver/blob/v2.21.0/src/MongoDB.Bson/PowerOf2.cs#L29

 

Comment by PM Bot [ 24/Aug/23 ]

Hi melvin.courjaud@e-i.com, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Wed Feb 07 21:49:19 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.