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

#### desgin

##### Member level 4
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).

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
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:
desgin

points: 2