Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

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

desgin

Member level 4
Joined
Apr 7, 2017
Messages
69
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
619
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 a moderator:

surerdra

Junior Member level 1
Joined
Feb 15, 2014
Messages
18
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Activity points
237
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 a moderator:
  • Like
Reactions: desgin

    desgin

    points: 2
    Helpful Answer Positive Rating
Toggle Sidebar

Part and Inventory Search


Welcome to EDABoard.com

Sponsor

Sponsor

Design Fast


×
Top