Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<arg>-Xlint:all</arg>
<arg>-Xlint:-auxiliaryclass</arg>
<arg>-Werror</arg>

</compilerArgs>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
package com.thealgorithms.bitmanipulation;
/**
* A utility class for performing single-bit operations on integers.
* These operations include flipping, setting, clearing, and getting
* individual bits at specified positions.
*
* Bit positions are zero-indexed (i.e., the least significant bit is at position 0).
* These methods leverage bitwise operations for optimal performance.
*
* Examples:
* - `flipBit(3, 1)` flips the bit at index 1 in binary `11` (result: `1`).
* - `setBit(4, 0)` sets the bit at index 0 in `100` (result: `101` or 5).
* - `clearBit(7, 1)` clears the bit at index 1 in `111` (result: `101` or 5).
* - `getBit(6, 0)` checks if the least significant bit is set (result: `0`).
*
* Time Complexity: O(1) for all operations.
*
* Author: lukasb1b (https://github.com/lukasb1b)
*/
public final class SingleBitOperations {
private SingleBitOperations() {
}
/**
* Flips (toggles) the bit at the specified position.
*
* @param num the input number
* @param bit the position of the bit to flip (0-indexed)
* @return the new number after flipping the specified bit
*/
public static int flipBit(final int num, final int bit) {
return num ^ (1 << bit);
}
/**
* Sets the bit at the specified position to 1.
*
* @param num the input number
* @param bit the position of the bit to set (0-indexed)
* @return the new number after setting the specified bit to 1
*/
public static int setBit(final int num, final int bit) {
return num | (1 << bit);
}
/**
* Clears the bit at the specified position (sets it to 0).
*
* @param num the input number
* @param bit the position of the bit to clear (0-indexed)
* @return the new number after clearing the specified bit
*/
public static int clearBit(final int num, final int bit) {
return num & ~(1 << bit);
}
/**
* Gets the bit value (0 or 1) at the specified position.
*
* @param num the input number
* @param bit the position of the bit to retrieve (0-indexed)
* @return 1 if the bit is set, 0 otherwise
*/
public static int getBit(final int num, final int bit) {
return (num >> bit) & 1;
}
}
package com.thealgorithms.bitmanipulation;

/**
* A utility class for performing single-bit operations on integers.
* These operations include flipping, setting, clearing, and getting
* individual bits at specified positions.
*
* Bit positions are zero-indexed (i.e., the least significant bit is at position 0).
* These methods leverage bitwise operations for optimal performance.
*
* Examples:
* - `flipBit(3, 1)` flips the bit at index 1 in binary `11` (result: `1`).
* - `setBit(4, 0)` sets the bit at index 0 in `100` (result: `101` or 5).
* - `clearBit(7, 1)` clears the bit at index 1 in `111` (result: `101` or 5).
* - `getBit(6, 0)` checks if the least significant bit is set (result: `0`).
*
* Time Complexity: O(1) for all operations.
*
* Author: lukasb1b (https://github.com/lukasb1b)
*/
public final class SingleBitOperations {
private SingleBitOperations() {
}

/**
* Flips (toggles) the bit at the specified position.
*
* @param num the input number
* @param bit the position of the bit to flip (0-indexed)
* @return the new number after flipping the specified bit
*/
public static int flipBit(final int num, final int bit) {
return num ^ (1 << bit);
}

/**
* Sets the bit at the specified position to 1.
*
* @param num the input number
* @param bit the position of the bit to set (0-indexed)
* @return the new number after setting the specified bit to 1
*/
public static int setBit(final int num, final int bit) {
return num | (1 << bit);
}

/**
* Clears the bit at the specified position (sets it to 0).
*
* @param num the input number
* @param bit the position of the bit to clear (0-indexed)
* @return the new number after clearing the specified bit
*/
public static int clearBit(final int num, final int bit) {
return num & ~(1 << bit);
}

/**
* Gets the bit value (0 or 1) at the specified position.
*
* @param num the input number
* @param bit the position of the bit to retrieve (0-indexed)
* @return 1 if the bit is set, 0 otherwise
*/
public static int getBit(final int num, final int bit) {
return (num >> bit) & 1;
}
}
202 changes: 101 additions & 101 deletions src/main/java/com/thealgorithms/ciphers/AtbashCipher.java
Original file line number Diff line number Diff line change
@@ -1,101 +1,101 @@
package com.thealgorithms.ciphers;
/**
* The Atbash cipher is a classic substitution cipher that substitutes each letter
* with its opposite letter in the alphabet.
*
* For example:
* - 'A' becomes 'Z', 'B' becomes 'Y', 'C' becomes 'X', and so on.
* - Similarly, 'a' becomes 'z', 'b' becomes 'y', and so on.
*
* The cipher works identically for both uppercase and lowercase letters.
* Non-alphabetical characters remain unchanged in the output.
*
* This cipher is symmetric, meaning that applying the cipher twice will return
* the original text. Therefore, the same function is used for both encryption and decryption.
*
* <p>Usage Example:</p>
* <pre>
* AtbashCipher cipher = new AtbashCipher("Hello World!");
* String encrypted = cipher.convert(); // Output: "Svool Dliow!"
* </pre>
*
* @author <a href="https://github.com/Krounosity">Krounosity</a>
* @see <a href="https://en.wikipedia.org/wiki/Atbash">Atbash Cipher (Wikipedia)</a>
*/
public class AtbashCipher {
private String toConvert;
public AtbashCipher() {
}
/**
* Constructor with a string parameter.
*
* @param str The string to be converted using the Atbash cipher
*/
public AtbashCipher(String str) {
this.toConvert = str;
}
/**
* Returns the current string set for conversion.
*
* @return The string to be converted
*/
public String getString() {
return toConvert;
}
/**
* Sets the string to be converted using the Atbash cipher.
*
* @param str The new string to convert
*/
public void setString(String str) {
this.toConvert = str;
}
/**
* Checks if a character is uppercase.
*
* @param ch The character to check
* @return {@code true} if the character is uppercase, {@code false} otherwise
*/
private boolean isCapital(char ch) {
return ch >= 'A' && ch <= 'Z';
}
/**
* Checks if a character is lowercase.
*
* @param ch The character to check
* @return {@code true} if the character is lowercase, {@code false} otherwise
*/
private boolean isSmall(char ch) {
return ch >= 'a' && ch <= 'z';
}
/**
* Converts the input string using the Atbash cipher.
* Alphabetic characters are substituted with their opposite in the alphabet,
* while non-alphabetic characters remain unchanged.
*
* @return The converted string after applying the Atbash cipher
*/
public String convert() {
StringBuilder convertedString = new StringBuilder();
for (char ch : toConvert.toCharArray()) {
if (isSmall(ch)) {
convertedString.append((char) ('z' - (ch - 'a')));
} else if (isCapital(ch)) {
convertedString.append((char) ('Z' - (ch - 'A')));
} else {
convertedString.append(ch);
}
}
return convertedString.toString();
}
}
package com.thealgorithms.ciphers;

/**
* The Atbash cipher is a classic substitution cipher that substitutes each letter
* with its opposite letter in the alphabet.
*
* For example:
* - 'A' becomes 'Z', 'B' becomes 'Y', 'C' becomes 'X', and so on.
* - Similarly, 'a' becomes 'z', 'b' becomes 'y', and so on.
*
* The cipher works identically for both uppercase and lowercase letters.
* Non-alphabetical characters remain unchanged in the output.
*
* This cipher is symmetric, meaning that applying the cipher twice will return
* the original text. Therefore, the same function is used for both encryption and decryption.
*
* <p>Usage Example:</p>
* <pre>
* AtbashCipher cipher = new AtbashCipher("Hello World!");
* String encrypted = cipher.convert(); // Output: "Svool Dliow!"
* </pre>
*
* @author <a href="https://github.com/Krounosity">Krounosity</a>
* @see <a href="https://en.wikipedia.org/wiki/Atbash">Atbash Cipher (Wikipedia)</a>
*/
public class AtbashCipher {

private String toConvert;

public AtbashCipher() {
}

/**
* Constructor with a string parameter.
*
* @param str The string to be converted using the Atbash cipher
*/
public AtbashCipher(String str) {
this.toConvert = str;
}

/**
* Returns the current string set for conversion.
*
* @return The string to be converted
*/
public String getString() {
return toConvert;
}

/**
* Sets the string to be converted using the Atbash cipher.
*
* @param str The new string to convert
*/
public void setString(String str) {
this.toConvert = str;
}

/**
* Checks if a character is uppercase.
*
* @param ch The character to check
* @return {@code true} if the character is uppercase, {@code false} otherwise
*/
private boolean isCapital(char ch) {
return ch >= 'A' && ch <= 'Z';
}

/**
* Checks if a character is lowercase.
*
* @param ch The character to check
* @return {@code true} if the character is lowercase, {@code false} otherwise
*/
private boolean isSmall(char ch) {
return ch >= 'a' && ch <= 'z';
}

/**
* Converts the input string using the Atbash cipher.
* Alphabetic characters are substituted with their opposite in the alphabet,
* while non-alphabetic characters remain unchanged.
*
* @return The converted string after applying the Atbash cipher
*/
public String convert() {
StringBuilder convertedString = new StringBuilder();

for (char ch : toConvert.toCharArray()) {
if (isSmall(ch)) {
convertedString.append((char) ('z' - (ch - 'a')));
} else if (isCapital(ch)) {
convertedString.append((char) ('Z' - (ch - 'A')));
} else {
convertedString.append(ch);
}
}
return convertedString.toString();
}
}
Loading
Loading