Skip to content

Instantly share code, notes, and snippets.

@Scherso
Last active April 4, 2023 16:36
Show Gist options
  • Save Scherso/c1659f7b7e376af40575eb000f53b3c1 to your computer and use it in GitHub Desktop.
Save Scherso/c1659f7b7e376af40575eb000f53b3c1 to your computer and use it in GitHub Desktop.
Uses for Bitwise operators
#include <stdio.h>
#include <stdlib.h>
/**
* Checking whether a number is even or odd,
*
* A base ten '2' is equal to a base-two '0010'.
* When using a bitwise 'and' on the numbers '0001'
* and '0010' there are no matching digits, meaning
* a return of only zeros.
*
* Pseudo-code
*
* bool isEven(int input) {
* if ((input & 0001) > 0000)
* return false
* if ((input & 0001) == 0000)
* return true
* }
*
* An example with an even and an odd number,
* | Comparee | Compared | Result |
* | 0010, 2 | 0001, 1 | 0 |
* | 0011, 3 | 0001, 1 | 1 |
* | 0100, 4 | 0001, 1 | 0 |
* | 0101, 5 | 0001, 1 | 1 |
* | 0110, 6 | 0001, 1 | 0 |
*/
int main()
{
// The number to test.
int x = 2;
if (x & 1)
printf("%d is odd.", x);
else
printf("%d is even.", x);
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x = 32;
/* If the number is less than
* zero, it's not a power of two. */
if (x < 0) return;
if ((x & (x - 1)) == 0)
printf("%d is a power of two", x);
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#define ASCII_MIN 65 // 01000001, 'A'
#define ASCII_MAX 90 // 01011010, 'Z'
#define NULL_TERM 0x00 // ASCII Null Terminator
#define ASCII_BITMASK 32 // 0x20, Bitmask between uppercase and lower values.
int main()
{
char str[] = "HELLO, WORLD";
to_lowercase(str);
printf("%s\n", str);
return EXIT_SUCCESS;
}
/**
* In the ASCII table, the decimal values for the characters
* 'A' to 'Z' are from 65 to 90, and 'a' to 'z' are from 97 to 122.
* This method requires that we have a 6th bit, (32 decimal ->
* which is '00100000' in binary) to 1 in ASCII.
* Our bitwise OR operator here, with the value 32 set to the 6th bit to one
* will effectively convert any uppercase characters to lowercase
* characters until the string null terminator '\0' is reached.
*
* @param str The inputted character array to be converted.
*/
void to_lowercase(char str[])
{
for (int i = 0; str[i] != NULL_TERM; ++i)
{
if (str[i] >= ASCII_MIN && str[i] <= ASCII_MAX)
str[i] |= ASCII_BITMASK;
}
}
#include <stdio.h>
#include <stdlib.h>
#define ASCII_MIN 97 // 01100001, 'a'
#define ASCII_MAX 122 // 01111010, 'z'
#define NULL_TERM 0x00 // ASCII Null Terminator
int main()
{
char str[] = "hello, world";
to_uppercase(str);
printf("%s\n", str);
return EXIT_SUCCESS;
}
/**
* Here, the bitwise AND operation is shifted with a
* negation of 1 shifted to the left by 5 bits.
* This sets the 6th bit, (5 if you're counting from 1) to 0,
* which is the bit that distinguishes lowercase letters from
* uppercase letters in proper ASCII.
*
* Here's an example of this shift:
* In this example we'll use the following,
* and it should output the next entrace in the table.
*
* CHARACTER ASCII VALUE BINARY VALUE
* 'a' 97 01100001
* TO --- ---- --------
* 'A' 65 01000001
*
* Bitwise Shift: 01100001 << 5 = 10000000
* Bitwise AND: 10000000 & 11011111 = 01000001
* 01000001: 65 in decimal, ASCII value for 'A'.
*
* @param str The inputted character array to be converted.
*/
void to_uppercase(char original[])
{
for (int i = 0; original[i] != NULL_TERM; ++i)
{
if (original[i] >= ASCII_MIN && original[i] <= ASCII_MAX)
original[i] &= ~(1 << 5);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment