Skip to content

Commit 0c81975

Browse files
committed
bits and english alphabet
1 parent a309809 commit 0c81975

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
- [x] [Bit Hacks — Part 1 (Basic)](http://www.techiedelight.com/bit-hacks-part-1-basic/)
5757
- [x] [Bit Hacks — Part 2 (Playing with k’th bit)](http://www.techiedelight.com/bit-hacks-part-2-playing-kth-bit/)
5858
- [x] [Bit Hacks — Part 3 (Playing with rightmost set bit of a number)](http://www.techiedelight.com/bit-hacks-part-3-playing-rightmost-set-bit-number/)
59-
59+
- [x] [Bit Hacks — Part 4 (Playing with letters of English alphabet)](http://www.techiedelight.com/bit-hacks-part-4-playing-letters-english-alphabet/)
60+
- [ ] [Bit Hacks — Part 5 (Find absolute value of an integer without branching)](http://www.techiedelight.com/bit-hacks-part-5-find-absolute-value-integer-without-branching/)
61+
- [ ] [Bit Hacks — Part 6 (Random Problems)](http://www.techiedelight.com/bit-hacks-part-6-random-problems/)
6062

6163
## Algorithms
6264

@@ -174,9 +176,6 @@
174176
## To Do
175177

176178
- Bits Manipulation
177-
- [ ] [Bit Hacks — Part 4 (Playing with letters of English alphabet)](http://www.techiedelight.com/bit-hacks-part-4-playing-letters-english-alphabet/)
178-
- [ ] [Bit Hacks — Part 5 (Find absolute value of an integer without branching)](http://www.techiedelight.com/bit-hacks-part-5-find-absolute-value-integer-without-branching/)
179-
- [ ] [Bit Hacks — Part 6 (Random Problems)](http://www.techiedelight.com/bit-hacks-part-6-random-problems/)
180179
- [ ] [Brian Kernighan’s Algorithm to count set bits in an integer](http://www.techiedelight.com/brian-kernighans-algorithm-count-set-bits-integer/)
181180
- [ ] [Compute parity of a number using lookup table](http://www.techiedelight.com/compute-parity-number-using-lookup-table/)
182181
- [ ] [Count set bits using lookup table](http://www.techiedelight.com/count-set-bits-using-lookup-table/)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package binary.letters;
2+
3+
public class UpperLowercaseSwapper {
4+
public static void main(String[] args) {
5+
// to lowercase
6+
for (char ch = 'A'; ch <= 'Z'; ch++) {
7+
System.out.print((char) (ch | ' ')); // OR space
8+
}
9+
System.out.println();
10+
// to uppercase
11+
for (char ch = 'a'; ch <= 'z'; ch++) {
12+
System.out.print((char) (ch & '_')); // AND underscore
13+
}
14+
15+
/* Why does this work?
16+
* Character ASCII (Decimal) ASCII (Binary) Changes
17+
'A' 65 01000001
18+
'a' 97 01100001 'A' → 'a' adds 32 → sets bit 5 (counting from 0)
19+
' ' 32 00100000 OR'ing with space sets bit 5, XOR'ing with space flips bit 5 so swaps the case
20+
'_' 95 01011111 AND'ing with underscore clears bit 5
21+
* */
22+
23+
System.out.println();
24+
System.out.println();
25+
// to invert
26+
// lowercase uppercase
27+
for (char ch = 'a'; ch <= 'z'; ch++) {
28+
System.out.print((char) (ch ^ ' ')); // XOR space
29+
}
30+
System.out.println();
31+
// same for uppercase to lowercase
32+
for (char ch = 'A'; ch <= 'Z'; ch++) {
33+
System.out.print((char) (ch ^ ' ')); // XOR space
34+
}
35+
36+
System.out.println();
37+
System.out.println();
38+
// find letter's position in alphabet - AND 31
39+
for (char ch = 'a'; ch <= 'z'; ch++) {
40+
System.out.println(ch + " is in alphabet index " + (ch & 31)); // AND 31 (00011111)
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)