Hacking with ANSI C signal Handling

$sudo strace -e trace=signal -p 1 
[sudo] password for jeffrin: 
Process 1 attached
kill(4659, SIGTERM)                     = 0
kill(4659, SIGCONT)                     = 0

$gcc crtlc1-p1.c 
$./a.out 
Hello world 
Hello world 
Hello world 
Hello world 
Hello world 
Hello world 
Hello world 
Hello world 
Hello world 
^Ci got a signal 2
Hello world 
Hello world 
^C
$cat crtlc1-p1.c 
#include 
#include 
#include 

void ouch(int sig)
{

  printf("i got a signal %d\n",sig);
  (void) signal(SIGINT, SIG_DFL);
}

int main()
{

  (void) signal(SIGINT,ouch);

  while(1)
    {
      printf("Hello world \n");
      sleep(1);
    }
}
$

How to write a program to multiply a number by 4 using a bitwise operator ?

/* Part of this program copied from http://www.sanfoundry.com/c-program-multiply-number-4-using-bitwise-operators/ */

#include <stdio.h>

multbitwise()
{

long number;
printf("Enter an integer: ");
scanf("%ld",&number);
number = number << 2;
printf("Result is %ld \n",number);
}

/* http://stackoverflow.com/questions/4456442/interview-multiplication-of-2-integers-using-bitwise-operators */
/* http://www.geeksforgeeks.org/multiply-two-numbers-without-using-multiply-division-bitwise-operators-and-no-loops/ */
/* http://en.wikipedia.org/wiki/Bitwise_operation */

Hacking with conversions of decimal and binary

$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
Finding if a number is an Armstrong number or not[8] 
Finding if a given number is prime number[9]  
Finding the number of twin prime numbers[10] 
Finding if a number is palindrome or not[11] 
Finding prime factor of a number[12] 
Finding the value of M^N[13] 
Finding factorial of a number[14] 
Finding the sum of the series 1! + 2! +...+N! [15] 
Finding the sume of the series 1 + 2 +...+N [16] 
Finding Parity of an Integer [17] 
Finding the biggest among three numbers [18] 
Finding the sum of even and odd numbers up to a number N [19] 
Finding the sum and number of integers divisible by 5 [20] 
Swapping Values [21] 
Finding the decimal value for a binary [22] 
Finding the reverse of an integer [23] 
Finding a way for user authentication [24]: 
Finding the binary equivalent of a decimal number and count no. of 1's in binary number [25] 
Option: 25
Enter a decimal integer 
25
 Input number is 25
 It's binary equivalent is 11001 
No. of 1s in the binary number is 3
$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
Finding if a number is an Armstrong number or not[8] 
Finding if a given number is prime number[9]  
Finding the number of twin prime numbers[10] 
Finding if a number is palindrome or not[11] 
Finding prime factor of a number[12] 
Finding the value of M^N[13] 
Finding factorial of a number[14] 
Finding the sum of the series 1! + 2! +...+N! [15] 
Finding the sume of the series 1 + 2 +...+N [16] 
Finding Parity of an Integer [17] 
Finding the biggest among three numbers [18] 
Finding the sum of even and odd numbers up to a number N [19] 
Finding the sum and number of integers divisible by 5 [20] 
Swapping Values [21] 
Finding the decimal value for a binary [22] 
Finding the reverse of an integer [23] 
Finding a way for user authentication [24]: 
Finding the binary equivalent of a decimal number and count no. of 1's in binary number [25] 
Option: 22
Enter a binary number : 11001
The binary number is : 11001 
It's decimal equivalent is:  25 
$

How to write an algorithm to reverse an integer ?

/* Part of this work is copied from http://www.sanfoundry.com/c-program-to-reverse-a-given-number/ */

#include <stdio.h>

revint()
{

long num, reverse = 0, temp, remainder;
printf("Enter the number \n");
scanf("%ld",&num);
temp = num;
while ( num > 0 )
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Given number = %ld \n", temp);
printf("It's reverse = %ld \n", reverse);
}

/* http://stackoverflow.com/questions/15349723/reversing-an-integer-in-java-using-a-for-loop */
/* http://math.stackexchange.com/questions/480068/how-to-reverse-digits-of-an-integer-mathematically */

 

Hacking with an algorithm to reverse an integer

$./app 
Sign detection [1] 
Power of two [2] 
Counting No. of bits set [3] 
Set or clear bits without branching[4] 
Find maximum value[5] 
Finding least common multiple[6] 
Finding the greatest commom divisor[7] 
Finding if a number is an Armstrong number or not[8] 
Finding if a given number is prime number[9]  
Finding the number of twin prime numbers[10] 
Finding if a number is palindrome or not[11] 
Finding prime factor of a number[12] 
Finding the value of M^N[13] 
Finding factorial of a number[14] 
Finding the sum of the series 1! + 2! +...+N! [15] 
Finding the sume of the series 1 + 2 +...+N [16] 
Finding Parity of an Integer [17] 
Finding the biggest among three numbers [18] 
Finding the sum of even and odd numbers up to a number N [19] 
Finding the sum and number of integers divisible by 5 [20] 
Swapping Values [21] 
Finding the decimal value for a binary [22] 
Finding the reverse of an integer [23] 
23
Enter the number 
47124907242424245782424578
Given number = 9223372036854775807 
It's reverse = 7085774586302733229 
$

How to write an algorithm to do a binary to decimal conversion ?

/* part of work copied from http://www.sanfoundry.com/c-program-binary-number-into-decimal/ */
#include <stdio.h>

btd()
{
int num, binary_val, decimal_val = 0, base = 1, rem;
printf("Enter a binary number : ");
scanf("%d",&num);
binary_val = num;
while ( num > 0 )
{
rem = num % 10;
decimal_val = decimal_val + rem * base;
num = num / 10;
base = base * 2;
}
printf("The binary number is : %d \n",binary_val);
printf("It's decimal equivalent is: %d \n",decimal_val);
}

/* http://numbermonk.com/ */
/* remainder -- a part that is still to come to make things perfect
(or make something working fully) */
/* http://www.mathsisfun.com/definitions/remainder.html */
/* "num" is considered as a number which has to be broken
into 10 pieces beacuse the resulting number is built
using a maximum of 10 building blocks.

How to write an algorithm to swap values ?

/* part of work copied from http://www.sanfoundry.com/c-program-swap-values/ */
#include <stdio.h>

swap_values()
{
int temp;
int *ptr1, *ptr2;
int a, b;
printf("Enter integer values to be swapped: ");
scanf("%d %d",&a,&b);
ptr1 = &a;
ptr2 = &b;
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
printf("Swapped values are: %d %d \n",a,b);
}

/* http://www.programmingsimplified.com/c-program-swap-two-numbers */
/* http://betterexplained.com/articles/swap-two-variables-using-xor/ */
/* http://www.science.unitn.it/~fiorella/guidelinux/tlk/node27.html */