Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Sep 13, 2025

Summary

Simplifies the RawNumberToAddressConverter<TLinkAddress> by directly clearing the most significant bit instead of using the complex Hybrid<TLinkAddress>.AbsoluteValue logic.

Changes Made

  • Simplified Conversion Logic: Replaced _converter.Convert(new Hybrid<TLinkAddress>(source).AbsoluteValue) with direct MSB clearing using bitwise AND operation
  • Removed Dependencies: Eliminated the need for UncheckedConverter<long, TLinkAddress> and Hybrid<TLinkAddress> instantiation
  • Performance Improvement: The new implementation is more efficient as it avoids object creation and complex hybrid number logic
  • Clear Intent: The code now directly expresses the intent to clear the MSB (set to 0) as suggested in the issue

Implementation Details

The new implementation:

public TLinkAddress Convert(TLinkAddress source)
{
    // Simplified: just clear the most significant bit to get a positive address
    var longValue = long.CreateTruncating(source);
    var clearedMSB = longValue & 0x7FFFFFFFFFFFFFFF; // Clear MSB using bitwise AND
    return TLinkAddress.CreateTruncating(clearedMSB);
}

The key insight is that the original Hybrid<TLinkAddress>.AbsoluteValue was essentially converting the unsigned value to signed, taking the absolute value, and converting back. This can be simplified to just clearing the most significant bit, which ensures the result is always interpreted as a positive value.

Testing

  • Added comprehensive tests in RawNumberToAddressConverterTests.cs to verify the MSB clearing behavior
  • Tests cover different unsigned integer types (ulong, uint, ushort, byte)
  • Verified that the simplified implementation produces correct results for edge cases

Compatibility

This change maintains the same public API while simplifying the internal implementation. The behavior should be equivalent for all practical use cases where the converter is used to generate positive address values.

Fixes #67

🤖 Generated with Claude Code

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #67
@konard konard self-assigned this Sep 13, 2025
- Replaced complex Hybrid<TLinkAddress>(source).AbsoluteValue logic with direct MSB clearing
- Uses bitwise AND with 0x7FFFFFFFFFFFFFFF to clear the most significant bit
- Removed unnecessary UncheckedConverter dependency
- Added comprehensive tests to verify the simplified implementation
- Fixes #67

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] This converter can be simplified the only thing that is required to be done is to set the most significant bit to 0 Simplify RawNumberToAddressConverter by clearing MSB directly Sep 13, 2025
@konard konard marked this pull request as ready for review September 13, 2025 08:48
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

This converter can be simplified the only thing that is required to be done is to set the most significant bit to 0

2 participants