Jumat, 20 Januari 2012

2

[C] Program Konversi Bilangan Biner ke Desimal

  • Jumat, 20 Januari 2012
  • Nurkholish Ardi Firdaus
  • Share
  • Ketika saya mempelajari tentang bab "Representasi Data" dalam buku pembelajaran bahasa assembly untuk x86 proccessor terdapat tantangan bagi pembaca untuk membuat program konversi bilangan biner kedalam representasi bilangan bulatnya dalam bahasa C untuk mengetes pemahaman para pembaca dalam memahami konsepnya. Ini memang buku yang sangat menarik untuk dibaca, yaitu "Assembly Language for x86 Processors 6th Edition", anda harus membacanya! ;). Berikut adalah source codenya menurut pemahaman saya sendiri. Hope u enjoy it! :)





    ////////////////////////////////////////////////////////////////////////////////
    ///                       UNIVERSITAS NEGERI SEMARANG                        ///
    ///                             Fakultas Teknik                              ///
    ///                         Jurusan Teknik Elektro                           ///
    ///                Pendidikan Teknik Informatika dan Komputer                ///
    ///                     coded by Nurkholish Ardi Firdaus                     ///
    ////////////////////////////////////////////////////////////////////////////////
    //
    // Program Name: Representasi Bilangan Biner Kedalam Bilangan Bulat
    // Reference   : [ebook] Assembly Language for x86 Processors 6th Edition
    //
    ////////////////////////////////////////////////////////////////////////////////
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    int main()
    {
        //////////////////////////////////////////
        //                                      //
        // Jika Unsigned, dimulai dari MSB      //
        //                                      //
        //////////////////////////////////////////
        const   int Most_Significant_Bit = 0;
        //////////////////////////////////////////
        //                                      //
        // Jika Signed, dimulai dari LSB        //
        //                                      //
        //////////////////////////////////////////
        const   int Least_Significant_Bit = 63;
        //////////////////////////////////////////
    
        int     i;
        int     bin[64] = {0};
        char    user_bin[64] = {'0'};
        char    *pC;
        int     begin;
        int     step;
    
        long long result = 0;
    
        printf("Masukkan Angka Biner: ");
        scanf("%64s",user_bin);              //64-bit: batas Quadword
        pC = user_bin + strlen(user_bin) - 1;
        // mencopy data biner bertipe char dari user kedalam array integer
        for (i=Least_Significant_Bit; i>=0; i--) {
            if (pC[0] == '0') break;
            if (pC[0] == '1') bin[i] = 1;
            pC--;
        }
    
        if (bin[Most_Significant_Bit] == 1) {  // if signed
            begin = Most_Significant_Bit;
            step = 1;
        }
        else {
            begin = Least_Significant_Bit;
            step = -1;
        }
    
        if (step == 1) {   // if signed
            result = -(unsigned long long) pow((double) 2, (double)63);
            for (i=begin+1; i<=Least_Significant_Bit; i += step) {
                if (bin[i] == 1) {
                    result += (unsigned long long) pow((double) 2, (double)abs(i-63));
                }
            }
        }
        else {
            result = 0;
            for (i=begin; i>=Most_Significant_Bit; i += step) {
                if (bin[i] == 1) {
                    result += (unsigned long long) pow((double) 2, (double)abs(i-63));
                }
            }
        }
    
        printf("%s","\nRepresentasi Angka Bulat dari \n");
        for (i=0; i<=63; i++) {
            if ((i % 4) == 0) putchar(' ');
            if ((i % 32) == 0) putchar('\n');
            printf("%d",bin[i]);
        }
        printf("nnAdalah: %lldn",result);
        return 0;
    }
    Jangan Lupa Komentar Yaa..
    Related Posts Plugin for WordPress, Blogger...

    2 Komentar Untuk “[C] Program Konversi Bilangan Biner ke Desimal”

    jubek soraya mengatakan...
    Selasa, 07 Februari 2012 pukul 13.01.00 WIB Reply To This Comment

    susahnya jika hitung hitungan begini,,jadi males


    Blogger mengatakan...
    Minggu, 23 Februari 2020 pukul 18.15.00 WIB Reply To This Comment

    You should see how my partner Wesley Virgin's adventure launches in this SHOCKING and controversial VIDEO.

    Wesley was in the army-and soon after leaving-he discovered hidden, "mind control" tactics that the CIA and others used to obtain whatever they want.

    As it turns out, these are the same secrets lots of famous people (notably those who "became famous out of nowhere") and the greatest business people used to become wealthy and famous.

    You probably know how you only use 10% of your brain.

    Mostly, that's because the majority of your brainpower is UNCONSCIOUS.

    Perhaps that thought has even taken place IN YOUR very own head... as it did in my good friend Wesley Virgin's head about seven years ago, while riding an unregistered, garbage bucket of a car without a driver's license and with $3.20 in his pocket.

    "I'm very frustrated with going through life paycheck to paycheck! When will I finally make it?"

    You've taken part in those thoughts, isn't it right?

    Your very own success story is waiting to start. Go and take a leap of faith in YOURSELF.

    Watch Wesley Virgin's Video Now!


    Posting Komentar

    Berikan komentar positif tentang artikel yang sederhana ini niscaya sobat akan mendapatkan balasannya. Hehehe

    Subscribe