# Flip upper case with lower case

**URL:** <https://discourse.processing.org/t/flip-upper-case-with-lower-case/17013>\
**Category:** Beginners\
**Created:** [January 10, 2020, 10:11pm UTC](https://discourse.processing.org/t/flip-upper-case-with-lower-case/17013 "2020-01-10T22:11:53Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [January 10, 2020, 10:43pm UTC](https://discourse.processing.org/t/flip-upper-case-with-lower-case/17013/3 "2020-01-10T22:43:36Z")

</div>

> [@Andiwnl](#):
>
> What I need is : Hello → hELLO

If we print the **binary()** form of ASCII letters:

> **[binary() / Reference](https://processing.org/reference/binary_.html)**
>
> Converts an int, byte, char, or color to a String containing the equivalent binary notation. For example, the color value produced by color(0, 102, 153, 25…

```auto
println(binary('A', 8), hex('A', 2), (byte) 'A'); // 01000001 41 65
println(binary('a', 8), hex('a', 2), (byte) 'a'); // 01100001 61 97

println(binary('Z', 8), hex('Z', 2), (byte) 'Z'); // 01011010 5A 90
println(binary('z', 8), hex('z', 2), (byte) 'z'); // 01111010 7A 122

exit();

```

We’re gonna notice the only diff. between an uppercase ASCII letter from its corresponding lowercase is that the 3rd bit of the latter is `1`.

We can create a CASE\_MASK for that 3rd bit like this:  
`static final byte CASE_MASK = 1 << 5; // 0b00100000`

That is, the bit `1` is left-shifted 5 times, which is the same as `0b00100000` in binary, `0x20` in hexadecimal and `32` in decimal.

Now, in order to use that bit-mask as a case bit flipper, we’re gonna need to use the operator `^`:

```auto
static final byte CASE_MASK = 1 << 5; // 0b00100000

void setup() {
  println(binary(CASE_MASK, 8), hex(CASE_MASK, 2), CASE_MASK); // 00100000 20 32
  println();

  println(binary('A', 8), hex('A', 2), (byte) 'A'); // 01000001 41 65
  println(binary('a', 8), hex('a', 2), (byte) 'a'); // 01100001 61 97

  println(binary('Z', 8), hex('Z', 2), (byte) 'Z'); // 01011010 5A 90
  println(binary('z', 8), hex('z', 2), (byte) 'z'); // 01111010 7A 122
  println();

  println((char) ('A' ^ CASE_MASK)); // 'a'
  println((char) ('a' ^ CASE_MASK)); // 'A'
  println((char) ('Z' ^ CASE_MASK)); // 'z'
  println((char) ('z' ^ CASE_MASK)); // 'Z'

  exit();
}

```

Now you just need to create a function which flips the case of a whole String. 😉

P.S.: Alternatively we can use the space `' '` character as the mask, b/c its ASCII value is `32`: 😳  
`static final byte CASE_MASK = ' '; // same as 1 << 5`

```auto
  println((char) ('A' ^ ' ')); // 'a'
  println((char) ('a' ^ ' ')); // 'A'
  println((char) ('Z' ^ ' ')); // 'z'
  println((char) ('z' ^ ' ')); // 'Z'

  exit();

```

---

_[View the full topic](https://discourse.processing.org/t/flip-upper-case-with-lower-case/17013)._
