Skip to content

Commit f998f62

Browse files
author
Divyansh Saxena
committed
Normalize line endings to LF
1 parent 801460e commit f998f62

File tree

19 files changed

+2002
-2001
lines changed

19 files changed

+2002
-2001
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf
Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,68 @@
1-
package com.thealgorithms.bitmanipulation;
2-
3-
/**
4-
* A utility class for performing single-bit operations on integers.
5-
* These operations include flipping, setting, clearing, and getting
6-
* individual bits at specified positions.
7-
*
8-
* Bit positions are zero-indexed (i.e., the least significant bit is at position 0).
9-
* These methods leverage bitwise operations for optimal performance.
10-
*
11-
* Examples:
12-
* - `flipBit(3, 1)` flips the bit at index 1 in binary `11` (result: `1`).
13-
* - `setBit(4, 0)` sets the bit at index 0 in `100` (result: `101` or 5).
14-
* - `clearBit(7, 1)` clears the bit at index 1 in `111` (result: `101` or 5).
15-
* - `getBit(6, 0)` checks if the least significant bit is set (result: `0`).
16-
*
17-
* Time Complexity: O(1) for all operations.
18-
*
19-
* Author: lukasb1b (https://github.com/lukasb1b)
20-
*/
21-
public final class SingleBitOperations {
22-
private SingleBitOperations() {
23-
}
24-
25-
/**
26-
* Flips (toggles) the bit at the specified position.
27-
*
28-
* @param num the input number
29-
* @param bit the position of the bit to flip (0-indexed)
30-
* @return the new number after flipping the specified bit
31-
*/
32-
public static int flipBit(final int num, final int bit) {
33-
return num ^ (1 << bit);
34-
}
35-
36-
/**
37-
* Sets the bit at the specified position to 1.
38-
*
39-
* @param num the input number
40-
* @param bit the position of the bit to set (0-indexed)
41-
* @return the new number after setting the specified bit to 1
42-
*/
43-
public static int setBit(final int num, final int bit) {
44-
return num | (1 << bit);
45-
}
46-
47-
/**
48-
* Clears the bit at the specified position (sets it to 0).
49-
*
50-
* @param num the input number
51-
* @param bit the position of the bit to clear (0-indexed)
52-
* @return the new number after clearing the specified bit
53-
*/
54-
public static int clearBit(final int num, final int bit) {
55-
return num & ~(1 << bit);
56-
}
57-
58-
/**
59-
* Gets the bit value (0 or 1) at the specified position.
60-
*
61-
* @param num the input number
62-
* @param bit the position of the bit to retrieve (0-indexed)
63-
* @return 1 if the bit is set, 0 otherwise
64-
*/
65-
public static int getBit(final int num, final int bit) {
66-
return (num >> bit) & 1;
67-
}
68-
}
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* A utility class for performing single-bit operations on integers.
5+
* These operations include flipping, setting, clearing, and getting
6+
* individual bits at specified positions.
7+
*
8+
* Bit positions are zero-indexed (i.e., the least significant bit is at position 0).
9+
* These methods leverage bitwise operations for optimal performance.
10+
*
11+
* Examples:
12+
* - `flipBit(3, 1)` flips the bit at index 1 in binary `11` (result: `1`).
13+
* - `setBit(4, 0)` sets the bit at index 0 in `100` (result: `101` or 5).
14+
* - `clearBit(7, 1)` clears the bit at index 1 in `111` (result: `101` or 5).
15+
* - `getBit(6, 0)` checks if the least significant bit is set (result: `0`).
16+
*
17+
* Time Complexity: O(1) for all operations.
18+
*
19+
* Author: lukasb1b (https://github.com/lukasb1b)
20+
*/
21+
public final class SingleBitOperations {
22+
private SingleBitOperations() {
23+
}
24+
25+
/**
26+
* Flips (toggles) the bit at the specified position.
27+
*
28+
* @param num the input number
29+
* @param bit the position of the bit to flip (0-indexed)
30+
* @return the new number after flipping the specified bit
31+
*/
32+
public static int flipBit(final int num, final int bit) {
33+
return num ^ (1 << bit);
34+
}
35+
36+
/**
37+
* Sets the bit at the specified position to 1.
38+
*
39+
* @param num the input number
40+
* @param bit the position of the bit to set (0-indexed)
41+
* @return the new number after setting the specified bit to 1
42+
*/
43+
public static int setBit(final int num, final int bit) {
44+
return num | (1 << bit);
45+
}
46+
47+
/**
48+
* Clears the bit at the specified position (sets it to 0).
49+
*
50+
* @param num the input number
51+
* @param bit the position of the bit to clear (0-indexed)
52+
* @return the new number after clearing the specified bit
53+
*/
54+
public static int clearBit(final int num, final int bit) {
55+
return num & ~(1 << bit);
56+
}
57+
58+
/**
59+
* Gets the bit value (0 or 1) at the specified position.
60+
*
61+
* @param num the input number
62+
* @param bit the position of the bit to retrieve (0-indexed)
63+
* @return 1 if the bit is set, 0 otherwise
64+
*/
65+
public static int getBit(final int num, final int bit) {
66+
return (num >> bit) & 1;
67+
}
68+
}
Lines changed: 101 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,101 @@
1-
package com.thealgorithms.ciphers;
2-
3-
/**
4-
* The Atbash cipher is a classic substitution cipher that substitutes each letter
5-
* with its opposite letter in the alphabet.
6-
*
7-
* For example:
8-
* - 'A' becomes 'Z', 'B' becomes 'Y', 'C' becomes 'X', and so on.
9-
* - Similarly, 'a' becomes 'z', 'b' becomes 'y', and so on.
10-
*
11-
* The cipher works identically for both uppercase and lowercase letters.
12-
* Non-alphabetical characters remain unchanged in the output.
13-
*
14-
* This cipher is symmetric, meaning that applying the cipher twice will return
15-
* the original text. Therefore, the same function is used for both encryption and decryption.
16-
*
17-
* <p>Usage Example:</p>
18-
* <pre>
19-
* AtbashCipher cipher = new AtbashCipher("Hello World!");
20-
* String encrypted = cipher.convert(); // Output: "Svool Dliow!"
21-
* </pre>
22-
*
23-
* @author <a href="https://github.com/Krounosity">Krounosity</a>
24-
* @see <a href="https://en.wikipedia.org/wiki/Atbash">Atbash Cipher (Wikipedia)</a>
25-
*/
26-
public class AtbashCipher {
27-
28-
private String toConvert;
29-
30-
public AtbashCipher() {
31-
}
32-
33-
/**
34-
* Constructor with a string parameter.
35-
*
36-
* @param str The string to be converted using the Atbash cipher
37-
*/
38-
public AtbashCipher(String str) {
39-
this.toConvert = str;
40-
}
41-
42-
/**
43-
* Returns the current string set for conversion.
44-
*
45-
* @return The string to be converted
46-
*/
47-
public String getString() {
48-
return toConvert;
49-
}
50-
51-
/**
52-
* Sets the string to be converted using the Atbash cipher.
53-
*
54-
* @param str The new string to convert
55-
*/
56-
public void setString(String str) {
57-
this.toConvert = str;
58-
}
59-
60-
/**
61-
* Checks if a character is uppercase.
62-
*
63-
* @param ch The character to check
64-
* @return {@code true} if the character is uppercase, {@code false} otherwise
65-
*/
66-
private boolean isCapital(char ch) {
67-
return ch >= 'A' && ch <= 'Z';
68-
}
69-
70-
/**
71-
* Checks if a character is lowercase.
72-
*
73-
* @param ch The character to check
74-
* @return {@code true} if the character is lowercase, {@code false} otherwise
75-
*/
76-
private boolean isSmall(char ch) {
77-
return ch >= 'a' && ch <= 'z';
78-
}
79-
80-
/**
81-
* Converts the input string using the Atbash cipher.
82-
* Alphabetic characters are substituted with their opposite in the alphabet,
83-
* while non-alphabetic characters remain unchanged.
84-
*
85-
* @return The converted string after applying the Atbash cipher
86-
*/
87-
public String convert() {
88-
StringBuilder convertedString = new StringBuilder();
89-
90-
for (char ch : toConvert.toCharArray()) {
91-
if (isSmall(ch)) {
92-
convertedString.append((char) ('z' - (ch - 'a')));
93-
} else if (isCapital(ch)) {
94-
convertedString.append((char) ('Z' - (ch - 'A')));
95-
} else {
96-
convertedString.append(ch);
97-
}
98-
}
99-
return convertedString.toString();
100-
}
101-
}
1+
package com.thealgorithms.ciphers;
2+
3+
/**
4+
* The Atbash cipher is a classic substitution cipher that substitutes each letter
5+
* with its opposite letter in the alphabet.
6+
*
7+
* For example:
8+
* - 'A' becomes 'Z', 'B' becomes 'Y', 'C' becomes 'X', and so on.
9+
* - Similarly, 'a' becomes 'z', 'b' becomes 'y', and so on.
10+
*
11+
* The cipher works identically for both uppercase and lowercase letters.
12+
* Non-alphabetical characters remain unchanged in the output.
13+
*
14+
* This cipher is symmetric, meaning that applying the cipher twice will return
15+
* the original text. Therefore, the same function is used for both encryption and decryption.
16+
*
17+
* <p>Usage Example:</p>
18+
* <pre>
19+
* AtbashCipher cipher = new AtbashCipher("Hello World!");
20+
* String encrypted = cipher.convert(); // Output: "Svool Dliow!"
21+
* </pre>
22+
*
23+
* @author <a href="https://github.com/Krounosity">Krounosity</a>
24+
* @see <a href="https://en.wikipedia.org/wiki/Atbash">Atbash Cipher (Wikipedia)</a>
25+
*/
26+
public class AtbashCipher {
27+
28+
private String toConvert;
29+
30+
public AtbashCipher() {
31+
}
32+
33+
/**
34+
* Constructor with a string parameter.
35+
*
36+
* @param str The string to be converted using the Atbash cipher
37+
*/
38+
public AtbashCipher(String str) {
39+
this.toConvert = str;
40+
}
41+
42+
/**
43+
* Returns the current string set for conversion.
44+
*
45+
* @return The string to be converted
46+
*/
47+
public String getString() {
48+
return toConvert;
49+
}
50+
51+
/**
52+
* Sets the string to be converted using the Atbash cipher.
53+
*
54+
* @param str The new string to convert
55+
*/
56+
public void setString(String str) {
57+
this.toConvert = str;
58+
}
59+
60+
/**
61+
* Checks if a character is uppercase.
62+
*
63+
* @param ch The character to check
64+
* @return {@code true} if the character is uppercase, {@code false} otherwise
65+
*/
66+
private boolean isCapital(char ch) {
67+
return ch >= 'A' && ch <= 'Z';
68+
}
69+
70+
/**
71+
* Checks if a character is lowercase.
72+
*
73+
* @param ch The character to check
74+
* @return {@code true} if the character is lowercase, {@code false} otherwise
75+
*/
76+
private boolean isSmall(char ch) {
77+
return ch >= 'a' && ch <= 'z';
78+
}
79+
80+
/**
81+
* Converts the input string using the Atbash cipher.
82+
* Alphabetic characters are substituted with their opposite in the alphabet,
83+
* while non-alphabetic characters remain unchanged.
84+
*
85+
* @return The converted string after applying the Atbash cipher
86+
*/
87+
public String convert() {
88+
StringBuilder convertedString = new StringBuilder();
89+
90+
for (char ch : toConvert.toCharArray()) {
91+
if (isSmall(ch)) {
92+
convertedString.append((char) ('z' - (ch - 'a')));
93+
} else if (isCapital(ch)) {
94+
convertedString.append((char) ('Z' - (ch - 'A')));
95+
} else {
96+
convertedString.append(ch);
97+
}
98+
}
99+
return convertedString.toString();
100+
}
101+
}

0 commit comments

Comments
 (0)