Find Characters Position in English Alphabet Using Bit Manipulation

package org.wesome.dsalgo;

public class CharacterPosition {

    public static int findCharacterPosition(char ch) {
        int position = ch & 31;
        System.out.println("character " + ch + " comes at position " + position);
        return position;
    }
}
package org.wesome.dsalgo;

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

import static org.wesome.dsalgo.CharacterPosition.findCharacterPosition;


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

    @Test
    void LowercaseToUppercaseTest2() {
        Assertions.assertEquals(26, findCharacterPosition('z'));
    }

    @Test
    void LowercaseToUppercaseTest3() {
        int postion = 1;
        for (int i = 97; i <= 122; i++) {
            Assertions.assertEquals(postion, findCharacterPosition((char) i));
            postion++;
        }
    }

    @Test
    void LowercaseToUppercaseTest4() {
        int postion = 1;
        for (int i = 65; i <= 90; i++) {
            Assertions.assertEquals(postion, findCharacterPosition((char) i));
            postion++;
        }
    }
}
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