Skip to content

[FEATURE REQUEST] :SetorUnset a bit program in Bit manipulation #5489

@laxmanp090404

Description

@laxmanp090404

What would you like to Propose?

I would like to add a program under Bit Manipulation topic.
The program aims to set or unset any bit and return the modified number;
This is **particularly useful ** in cases like converting the case of a an alphabet by setting or unsetting bits.

Issue details

Name of the Algorithm

**Set or Unset Specific Bit **

Problem Statement

Implement a function in Java that can set or unset a specific bit of a given number. This is particularly useful for manipulating bits in various scenarios, such as converting between uppercase and lowercase letters by modifying specific bits (e.g., changing 'A' to 'a' or vice versa). The function should accept three arguments: the number, the bit position, and a flag to indicate whether to set or unset the bit.

Function Signature:

public class BitManipulation {
    public static int modifyBit(int number, int bitPosition, boolean setBit) {
      
    }
}
  • number: The integer whose bit will be modified.
  • bitPosition: The 0-indexed position of the bit to modify.
  • setBit: A boolean flag to indicate whether to set (true) or unset (false) the bit.

Algorithm Enhancements

  1. Adding Tests:

    • Comprehensive test cases to cover:
      • Setting and unsetting bits at various positions.
      • Alphabet case conversions by setting/unsetting the 5th bit of characters (e.g., converting A to a and a to A).
      • Edge cases like negative numbers and bit positions outside the valid range.

    Sample Test Cases :

    import static org.junit.jupiter.api.Assertions.*;
    import org.junit.jupiter.api.Test;
    
    public class BitManipulationTest {
        @Test
        public void testSetBit() {
            assertEquals(5, BitManipulation.modifyBit(1, 2, true)); // 1 to 5: 0001 -> 0101
        }
    
        @Test
        public void testUnsetBit() {
            assertEquals(1, BitManipulation.modifyBit(5, 2, false)); // 5 to 1: 0101 -> 0001
        }
    
        @Test
        public void testConvertUpperToLower() {
            assertEquals('a', BitManipulation.modifyBit('A', 5, true)); // ASCII: 'A' -> 'a'
        }
    
        @Test
        public void testConvertLowerToUpper() {
            assertEquals('A', BitManipulation.modifyBit('a', 5, false)); // ASCII: 'a' -> 'A'
        }
    }
  2. Optimizing Logic:

    • Use bitwise operations (| for setting and & ~ for unsetting) efficiently to minimize computational overhead.
    • Avoid using conditional loops or redundant checks to ensure that the logic remains time-efficient.
  3. Refactoring the File and Folder Structure:
    Organize the project with separate folders for code and tests. Structure it as follows:

    - bit_manipulation/
        - src/
            - BitManipulation.java
        - test/
            - BitManipulationTest.java
    

By following this structure, the code and tests will be separated for maintainability and ease of execution.

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions