Change Character Case Using Bit Manipulation

package org.wesome.dsalgo;

public class ToggleCase {
    public static final char SPACE = ' ';
    public static char a = 97;
    public static char A = 65;

    public static char toggleCase(char ch) {
        char character = (char) (ch ^ SPACE);
        System.out.println("original character " + ch + " toggled character " + character);
        return character;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.stream.IntStream;

import static org.wesome.dsalgo.ToggleCase.A;
import static org.wesome.dsalgo.ToggleCase.a;
import static org.wesome.dsalgo.ToggleCase.toggleCase;


public class CharacterPositionTest {
    @Test
    void LowercaseToUppercaseTest1() {
        Assertions.assertEquals('A', toggleCase('a'));
    }

    @Test
    void LowercaseToUppercaseTest2() {
        Assertions.assertEquals('a', toggleCase('A'));
    }

    @Test
    void LowercaseToUppercaseTest3() {
        Assertions.assertEquals('z', toggleCase('Z'));
    }

    @Test
    void LowercaseToUppercaseTest4() {
        Assertions.assertEquals('Z', toggleCase('z'));
    }

    @Test
    void UppercaseToLowercaseTest3() {
        IntStream.rangeClosed(65, 90).forEach(i -> {
            Assertions.assertEquals(a, toggleCase((char) i));
            a++;
        });
    }

    @Test
    void UppercaseToLowercaseTest4() {
        IntStream.rangeClosed(97, 122).forEach(i -> {
            Assertions.assertEquals(A, toggleCase((char) i));
            A++;
        });
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.4.1"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on