Pages - Menu

Showing posts with label bitwise. Show all posts
Showing posts with label bitwise. Show all posts

Monday, 8 October 2012

Case conversion using bitwise operators

For a better understanding of this article, you might want to read first:

Lowercase to uppercase
By looking at the ASCII code chart, you can see that the uppercase letters have values 32 less than the ones of the lowercase letters. This means that to convert a string to uppercase, it is enough to set the sixth bit to 0. 
The C function below changes all the letters of the string received as argument to uppercase characters.
/*
 * Description:
 *  Converts all the letters of a string to uppercase using bitwise operations
 * Parameters:
 *  string - the string to be converted to uppercase
 * Returns:
 *  converted - the string converted to uppercase
 */
char *ToUppercase(char *string)
{
    char *converted = NULL;
    int  i = 0;
    converted = (char*)malloc(sizeof(char) * strlen(string) + 1);
    for(i=0; i<strlen(string); i++)
    {
        if(string[i] >='a' && string[i] <='z')
        {
            converted[i] = (char) (string[i] & 65503);//65 503 (decimal) = 1111 1111 1101 1111 (binary)
        }
        else
        {
            converted[i] = string[i];
        }
    }
    converted[i] = '\0';
    return converted;
}
Example:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

char *ToUppercase(char *string);

int main(void)
{
    char s[] = "VINTAGE CODING";
    char *conv = NULL;
    printf("\n The string before lowercase conversion: %s",s);
    conv = ToLowercase(s);
    printf("\n\n\tThe string after lowercase conversion: %s",conv);
    getch();
    return 0;
}
Output: 

Uppercase to lowercase
For a conversion to lowercase of a string, you have to set the sixth bit to 1. The C function below changes all the letters of the string received as argument to lowercase characters.
/*
 * Description:
 *  Converts all the letters of a string to lowercase using bitwise operations
 * Parameters:
 *  string - the string to be converted to lowercase
 * Returns:
 *  converted - the string converted to lowercase
 */
char *ToLowercase(char *string)
{
    char *converted = NULL;
    int i = 0;
    converted = (char*)malloc(sizeof(char) * strlen(string) + 1);
    for(i=0; i<strlen(string); i++)
    {
        if(string[i] >= 'A' && string[i] <= 'Z')
        {
            converted[i] = (char) (string[i] | 32);//32 (decimal) = 0000 0000 0010 0000 (binary)
        }
        else
        {
            converted[i] = string[i];
        }
    }
    converted[i] = '\0';
    return converted;
}
Example:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

char *ToLowercase(char *string);

int main(void)
{
    char s[] = "VINTAGE CODING";
    char *conv = NULL;
    printf("\n The string before lowercase conversion: %s",s);
    conv = ToLowercase(s);
    printf("\n\n\tThe string after lowercase conversion: %s",conv);
    getch();
    return 0;
}
Output:  

Binary to decimal and decimal to binary base conversion using bitwise operators in C

The six operators that C provides are shown in the table below:
Operator
Action
&
Bitwise AND
|
Bitwise OR
^
Bitwise exclusive OR (XOR)
<< 
Shift left
>> 
Shift right
~
One's complement (NOT)
The bitwise AND, OR and NOT have the same truth tables as their logical equivalents, except that they work bit by bit. The outcome of an XOR operation is true if only one of the operands is true and false otherwise.
The bit-shift operators << and >> move all the bits of the variable to the left or right, as specified, the number of positions shifted being given by the right operand, which must be a positive integer. A shift is not a rotate, so the bits shifted do not come back at the other end, but are lost.
Bitwise operations may be applied only to integral operands like the standard char, short, int, and long data types, signed or unsigned.

For the truth table of the logical operators, read this article: 
Printing the truth table of the logical operators in C#                                  

Decimal to binary radix conversion
The following C function converts an integer from base 10 to binary. The first element of the bin array represents the sign bit.
#define MAX 17

/*
 * Description:
 *  Converts an integer from base 10 to base 2
 * Parameters:
 *  dec - the integer to be converted
 *  bin - pointer to the array that will contain the integer converted in binary
 * Returns:
 *  bin - array containing the binary equivalent of the integer to be converted
 */
int *DecimalToBinary(int dec, int *bin)
{
    int i = MAX;
    if(dec < 0)
    {
        dec = -dec;
        bin[0] = 1;
    }
    else
    {
        bin[0] = 0;
    }
    for(; i>=0; i--)
    {
        bin[MAX-i+1] = (dec>>i) & 1;
    }
    return bin;
}
Example:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main(void)
{
    int dec = 0, *bin = NULL, i = 0;
    bin = (int*)malloc(sizeof(int)*MAX);
    printf("\n Enter the number in decimal: ");
    scanf("%d",&dec);
    printf("\n\tThe number in binary: ");
    if(dec >= -100)
    {
        bin = DecimalToBinary(dec,bin);
        for(; bin[i] == 0 || bin[i] == 1; i++)
        {
            printf("%d",bin[i]);
        }
    }
    getch();
    return 0;
}
Output:


Binary to decimal radix conversion
The following C function converts an integer from base 2 to decimal. The first element of the bin array represents the sign bit.
#define MAX 17

/*
 * Description:
 *  Converts an integer from base 2 to base 10
 * Parameters:
 *  bin - array containing the integer in binary
 * Returns:
 *  decimal - the equivalent integer in base 10
 */
int BinaryToDecimal(int bin[])
{
    int i = 1, decimal = 0;
    for(; bin[i] == 0 || bin[i] == 1; i++)
    {
        decimal = (decimal<<1) | bin[i];
    }
    if(bin[0] == 1)
    {
        decimal = -decimal;
    }
    return decimal;
}
Example:
#include<stdio.h>
#include<conio.h>

int BinaryToDecimal(int bin[]);

int main(void)
{
    int bin[MAX], dec = 0, b = 0, i = 0;
    printf("\n Enter the number in binary (to stop reading, enter a number other than 0 or 1:\n");
    do
    {
        scanf("%d",&b);
        if(b == 0 || b == 1)
        {
            bin[i++] = b;
        }
    }while(b == 0 || b == 1);
    dec = BinaryToDecimal(bin);
    printf("\n\tThe number in decimal is: %d",dec);
    getch();
    return 0;
}
Output: