Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Please see the follwing pseudocode. This works fine with 32 bit inputs.</p> <h1>code</h1> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;math.h&gt; #define USERSIZE 128 long long int bin2dec(long long int ip) { int count, calc; long long int add = 0; for (count = 0; count &lt; 64; count++) calc = ip%10; add += pow(2, count) * calc; ip = ip/10; if (ip == 0) break; } return add; } int main() { int len = -1, maskval = -1; long long int temp; long long int intLeft; long long int intRight; char * input, *left, *right; input = calloc (USERSIZE, sizeof *input); left = calloc (USERSIZE/2, sizeof *left); right = calloc (USERSIZE/2, sizeof *right); printf("Enter the number in binary\n"); scanf("%s", input); len = strlen (input); if (len&gt; 0) { if (len%2 == 0) { maskval = len/2; strncpy(left, input, maskval); temp = atoll(left); intLeft = bin2dec(temp); printf("left = %lld (%lld)\n", intLeft, temp); input += maskval; strncpy(right, input, maskval); temp = atoll(right); intRight = bin2dec(temp); printf("right = %lld (%lld)\n", intRight, temp); } else printf("This number does not have even number of bits, pleease add a 0 before the number and enter again\n"); } else printf("Enter a valid string\n"); return 0; #endif } </code></pre> <h1>output</h1> <pre><code>[sourav@infba01383 so_overflow]# ./a.out Enter the number in binary 100 This number does not have even number of bits, pleease add a 0 before the number and enter again [sourav@infba01383 so_overflow]# ./a.out Enter the number in binary 0100 left = 1 (1) right = 0 (0) [sourav@infba01383 so_overflow]# ./a.out Enter the number in binary 1000 left = 2 (10) right = 0 (0) [sourav@infba01383 so_overflow]# ./a.out Enter the number in binary 0000 left = 0 (0) right = 0 (0) [sourav@infba01383 so_overflow]# ./a.out Enter the number in binary 1111 left = 3 (11) right = 3 (11) [sourav@infba01383 so_overflow]#./a.out Enter the number in binary 11111111111111111111111111111111 left = 65535 (1111111111111111) right = 65535 (1111111111111111) [sourav@infba01383 so_overflow]# </code></pre> <p>Beware!!! there is no check for the validity of the inputs.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload