Month: February 2016

Home / Month: February 2016

GMP (GNU Multi Precision) Library is one of vital library in GNU infrastructure. It is a popular library which gives flexibility to operate abritrary precision integers, rationals, and floating point numbers. There are also MPFR (Multi Precision Floating-point Reliably) and MPC (Multi Precision Complex) which are extension to to GMP capability for precision in computation.

In this article we will try to code using GMP, with C, on Linux 64-bit.


Check whether our box has GMP library.

locate libgmp.a

If GMP is installed already, we will get entry. If not, proceed to GMP site to download and install the library. Just do usual Linux installation procedure.

Getting Start

Before we start, let’s talking about the arbritrary precision. Do we need it? or why do we need it?

Program constitute of two element: algorithm and data. Data is represented by variable, which has size on memory. The size is fix, according to the word size of processor register. To simplify it, computer process numbers in multiple of 2: 8, 16, 32, or 64 bits at a time. For example, if we declare an int variable x, it will have 32 bit size.

Let’s discover by writing little code.

#include <stdio.h>
#include <limits.h>

void printbin(int x)
    if (x==0)
        if (x&1)

int main() {
    int k, x, i;
    char c;

    /* 1. Print machine native size and the max/min integer */
    printf("sizeof(int) = %d bits\n", sizeof(int) * 8);
    printf("max(int)    = %d\n", INT_MAX);
    printf("min(int)    = %d\n", INT_MIN);

    /* 2. Try to overflow */
    k = INT_MAX;

    /* 3. See the representation of variable */
    printf("Enter x: ");
    scanf("%d", &x);
    printf("x in binary = ");

    return 0;

sizeof(int) gives the number of bytes for integer and INT_MAX is a macro defined in limits.h which inform the largest possible integer. INT_MIN is just the opposite of it.

This is the result in my box.


See the range, -2147483648 (min) to 2147483647 is the valid value of 32-bits integer. If we increment x which hold maximum value it will overflow to negative.

Enter GMP!

Now let’s dive to the GMP. GMP allows us to use integers whose sizes can grow dynamically to the required precision. Imagine it as BigNum, close to. It can support 128, 256, or 1024 bits. There is no need for us to specify this, the library does to dirt. GMP will dynamically allocate memory to accommodate extra bits of precision as we need.

Let’s try it with simple code.

#include <gmp.h>
#include <stdio.h>
int main()
    char inputStr[1025];
    /* type for GMP integers. 
        We don't care about the internal representation 
    mpz_t n;
    int flag;
    printf ("Enter your number: ");
    scanf("%1024s" , inputStr);
      /* 1. Initialize the number */

    flag = mpz_set_str(n,inputStr, 10);
    if (flag == 0)
        return -1;
    printf ("n = ");
    printf ("\n");

    /* 3. Add one to the number */

    /* 4. Print the result */
    printf (" n +1 = ");
    printf ("\n");

    /* 5. Square n+1 */
    printf ("\n");


    return 0; 

Remember to include library libgmp.a so GCC won’t complaint. Here is the result:


The complete information is always on GMP manual. But we love simplicity. So here we go.

At minimum we need this skeleton to use GMP.

#include <gmp.h>

/* ... */

  mpz_t n;

      /* ... */


      /* ... */


Now let’s make something out of it. Say, factorial program?

#include <gmp.h>
#include <stdio.h>
#include <stdlib.h>

/* Use iterative solution here. */
void factorial(int n)
    int i;
    mpz_t p;
        mpz_mul_ui(p, p, i);    /* p = p * 1 */
    printf("%d! = ", n);
    mpz_out_str(stdout, 10, p);

int main()
    int n;
    do {
        printf("N = ");
        scanf("%d", &n);
        if (n <= 1)
            printf("N should be > 1\n");
    } while (1);
    return 0;

Try it!

Capturing USB Data with Wireshark

February 6, 2016 | Article | 1 Comment



  • Wireshark 2.0.1 (SVN)
  • Linux kernel 4.1.6


Some Knowledge


  • USB Memory














addgroup usbmon
gpasswd -a $USER usbmon


modprobe usbmon



The Wireshark Network Analyzer (as superuser)_048


-usbmon3 (as superuser)_049



Social media & sharing icons powered by UltimatelySocial