Thursday, February 26, 2015

Bit Manipulation in C and C++

General C Programming Courses

http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/c_tutorial.html


Basic Binary to Decimal and Decimal to Binary and Hexadecimal Conversion Algorithms

URL: http://courses.cse.tamu.edu/jmichael/f14/113/slide/csce113-data_rep.pdf


Floating Point Errors in C++

URL: http://www.cprogramming.com/tutorial/floating_point/understanding_floating_point_representation.html

++ Bit Manipulation

URL: http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.php#displaybit

C++ Online Compiler GNU GCC version 4.7.2

URL: http://www.tutorialspoint.com/cplusplus/index.htm

The programs at bogotogo.com run on tuturalspoint.com C ++ Compiler


C Programming

The C Programs at

URL: http://gribblelab.org/CBootcamp/3_Basic_Types_Operators_And_Expressions.html

Work on the C Compiler at

URL: http://www.tutorialspoint.com/cprogramming/c_environment_setup.htm

For example, running the program below

#include <stdio.h>

int main(int argc, char *argv[]) {
        printf("a char is %ld bytes\n", sizeof(char));
        printf("an int is %ld bytes\n", sizeof(int));
        printf("an float is %ld bytes\n", sizeof(float));
        printf("a double is %ld bytes\n", sizeof(double));
        printf("a short int is %ld bytes\n", sizeof(short int));
        printf("a long int is %ld bytes\n", sizeof(long int));
        printf("a long double is %ld bytes\n", sizeof(long double));
        return 0;
}

TutorialsPoint C Compiler, the following results are obtained

Compiling the source code....
$gcc main.c -o demo -lm -pthread -lgmp -lreadline 2>&1

Executing the program....
$demo 
a char is 1 bytes
an int is 4 bytes
an float is 4 bytes
a double is 8 bytes
a short int is 2 bytes
a long int is 8 bytes
a long double is 16 bytes
Bit Wise Operations in C

http://www.programiz.com/article/bitwise-operator-c-programming

C to Assembly Cross Compiler

http://gcc.godbolt.org/

C Online Compiler

http://coliru.stacked-crooked.com/




http://www.cs.cf.ac.uk/Dave/C/node4.html


Decimal to Binary Converter


int main()
{
  int n, c, k;

n = 1000000;

//prints 11110100001001000000
//checks 11110100001001000000 against online decimal to binary converter at
//http://www.binaryhexconverter.com/decimal-to-binary-converter

  printf("%d in binary number system is:\n", n);

  for (c = 31; c >= 0; c--)
  {
    k = n >> c; //bit wise shift operators

    if (k & 1)
      printf("1");
    else
      printf("0");
  }

  printf("\n");

  return 0;
}


SOURCES: http://www.programmingsimplified.com/c/source-code/c-program-convert-decimal-to-binary

Tested at URL:

http://www.tutorialspoint.com/cprogramming/c_environment_setup.htm

Output program

Executing the program....
$demo
1000000 in binary number system is:
00000000000011110100001001000000



Another Apprach to Decimal to Binary Conversion. Test for Overflow

#include <iostream>
using namespace std;


int main()
{
      int d[20];
      int decimalno,i=0;
      decimalno = 10000002020;
      int var1 = decimalno;
      while(decimalno>0)
      {
           d[i]=decimalno%2;
           i++;
           decimalno=decimalno/2;
      }
      for(int j=i-1;j>=0;j--)
      {
            printf("%d",d[j]);
      }
      printf(" is the binary version of the number you input, %d", var1);
 }


CONVERTING  A STRING TO AN INTEGER IN C PROGRAMMING - Using atoi function

#include <stdio.h>

#include <stdlib.h>

main()

{

char x[10] = "110";

int result = atoi(x);

printf("integer value of the string is %d\n", result/2);

}



Binary to Decimal Conversion Code

http://www.programiz.com/c-programming/examples/binary-decimal-convert

SOURCE CODE FOLLOWS


/* C programming source code to convert either binary to decimal or decimal to binary according to data entered by user. */

#include <stdio.h>
#include <math.h>
int binary_decimal(int n);
int decimal_binary(int n);
int main()
{
   int n;
   char c;
   printf("Instructions:\n");
   printf("1. Enter alphabet 'd' to convert binary to decimal.\n");
   printf("2. Enter alphabet 'b' to convert decimal to binary.\n");
   scanf("%c",&c);
   if (c =='d' || c == 'D')
   {
       printf("Enter a binary number: ");
       scanf("%d", &n);
       printf("%d in binary = %d in decimal", n, binary_decimal(n));
   }
   if (c =='b' || c == 'B')
   {
       printf("Enter a decimal number: ");
       scanf("%d", &n);
       printf("%d in decimal = %d in binary", n, decimal_binary(n));
   }
   return 0;
}

int decimal_binary(int n)  /* Function to convert decimal to binary.*/
{
    int rem, i=1, binary=0;
    while (n!=0)
    {
        rem=n%2;
        n/=2;
        binary+=rem*i;
        i*=10;
    }
    return binary;
}

int binary_decimal(int n) /* Function to convert binary to decimal.*/

{
    int decimal=0, i=0, rem;
    while (n!=0)
    {
        rem = n%10;
        n/=10;
        decimal += rem*pow(2,i);
        ++i;
    }
    return decimal;
}

No comments:

Post a Comment