+ Post New Thread
Results 1 to 2 of 2
  1. #1
    Member level 4
    Points: 770, Level: 6

    Join Date
    Apr 2017
    Posts
    68
    Helped
    1 / 1
    Points
    770
    Level
    6

    [Moved]Separation of Each bits in 8 bit long(data type) Binary number in mikro c Pro

    Hello All,

    I want to convert long integer to (long)Binary and then separate each bit from my (long data type)Binary number into 8 long variables.
    Not able to get correct output by below code, First tried in C (with Online compiler).

    Please help me!

    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    #include <stdio.h>
     
    void main()
    {
        long num=95,remainder, base = 1, binary = 0;
     
        while (num > 0)
        {
            remainder = num % 2;
            binary = binary + remainder * base;
            num = num / 2;
            base = base * 10;
        }
        printf("Its binary equivalent is = %ld\n", binary);
        binary >> 7;
        binary & 1;
        printf("New Binary: = %ld\n", binary);
    }
    Last edited by BradtheRad; 11th September 2019 at 06:04. Reason: Added code formatted window

    •   AltAdvertisement

        
       

  2. #2
    Junior Member level 1
    Points: 1,117, Level: 7

    Join Date
    Feb 2014
    Posts
    18
    Helped
    1 / 1
    Points
    1,117
    Level
    7

    Re: Separation of Each bits in 8 bit long(data type) Binary number in mikro c Pro

    the main difference between int and long int is range only, the number which is not crossed the range of int is same in long also, except while we convert from decimal to binary, we have to add
    zero's at remaining positions.

    ex: int 95 --> 00000000 00000000 00000000 01011111 in binary
    long 95 --> 00000000 00000000 00000000 00000000 00000000 00000000 00000000 01011111 in binary
    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    
    ///////////////////////////////////////////////
    in below code i contains the number of digits in binary representation. then you can add remaining higher bits with zero's like 
    : in above code i = 7(no of bits in binary i.e., 1011111) 
       binNum[64:7] <= 0;
     
     
     
     
    // function to convert decimal to binary 
    void decToBinary(int n) 
    { 
        // array to store binary number 
        long  binaryNum[64]; 
      
        // counter for binary array 
        int i = 0; 
        while (n > 0) { 
      
            // storing remainder in binary array 
            binaryNum[i] = n % 2; 
            n = n / 2; 
            i++; 
        } 
      
        // printing binary array in reverse order 
        for (int j = i - 1; j >= 0; j--) 
            cout << binaryNum[j]; 
    }
    Last edited by FvM; 11th September 2019 at 08:18. Reason: Added syntax tags


    1 members found this post helpful.

--[[ ]]--