| | |
Binary to Decimal Conversion
0
•
•
•
•
Nice program!!!
i'm favorly new to C and was wondering how the
line b<<(len-k) worked... all i know its shifts the digits to the left..
but what i dont get is by how much..
....ie. bin<<1 =multiply it by 2 so what does len-k do in the above statement..
hope that doesnt' sound confusing..
and also what does teh sum stand for in "sum=sum+n*b"
cheers
i'm favorly new to C and was wondering how the
line b<<(len-k) worked... all i know its shifts the digits to the left..
but what i dont get is by how much..
....ie. bin<<1 =multiply it by 2 so what does len-k do in the above statement..
hope that doesnt' sound confusing..
and also what does teh sum stand for in "sum=sum+n*b"
cheers
0
•
•
•
•
your code is much long.. i found one in net
try it
try it
C Syntax (Toggle Plain Text)
long bin2dec(char *s) { long r=0; for (; *s; r = (r<<1) | (*s++ - '0')); return r; }
Last edited by Nick Evan; Nov 20th, 2009 at 1:34 pm. Reason: Added code-tags
0
•
•
•
•
Good for you, but shorter is not always better. There is no error trapping, or explanation/comment how it works!
0
•
•
•
•
binary 10010 is calculated as 1*16+0*8+0*4+1*2+0*1 = decimal 18
hence b = b<<(len-k) does the multiplication by 1,2,4,8,16 ...
sum = sum + n * b;
// this will explain it ...
printf("%d*%d + ",n,b);
hence b = b<<(len-k) does the multiplication by 1,2,4,8,16 ...
sum = sum + n * b;
// this will explain it ...
printf("%d*%d + ",n,b);
0
•
•
•
•
C Syntax (Toggle Plain Text)
/*conversion of binary number to decimal*/ #include <stdio.h> int main() { int binary, digit, base = 0, decimal = 0; printf("enter a binary number: "); scanf("%d", &binary); printf("Entered binary number is: %d\n", binary); while(binary) { digit = binary % 10; decimal += digit << base; base += 1; binary /= 10; } printf("decimal equivalent of the entered binary number is: %d", decimal); return 0; }
Last edited by Nick Evan; Nov 20th, 2009 at 1:33 pm.
-2
•
•
•
•
//decimal to binary
C Syntax (Toggle Plain Text)
#include<stdio.h> void main() { long int deci,rem,bin,p=1; printf("enter no"); scanf("%ld",&deci); if (deci > 0) { while (deci > 0) { rem=deci%2; bin=rem*p+bin; p=p*10; deci=deci%2; } printf("binary no is %ld",bin); //end of while loop getchar(); } //end of condition else printf("binary no for +tive integer only"); getchar(); }
Last edited by adatapost; Feb 6th, 2010 at 9:43 am. Reason: Added [code] tags. Encase your code in: [code] and [/code] tags.
Similar Threads
Other Threads in the C Forum
- Code Snippet: Decimal to Binary conversion (C)
- binary to decimal conversion!! (C++)
- decimal to binary conversion (C++)
- decimal to binary conversion (C++)
- Help! Decimal to binary conversion (C)
Other Threads in the C Forum
- Previous Thread: error: expected unqualified-id before '(' token
- Next Thread: Getting a value from another .c file
| Thread Tools | Search this Thread |
Tag cloud for binary, conversion, decimal
angle array base binary bits c++ class code conversion convert converter count data decimal degrees delete digit encryption excel feet file function gray i/o impress inches input int integer java math millimeter mlm number numbertoword ofstream openoffice password pdf pointer python radians radix recursion recursive roman roman2decimal search string temperature time tree unit validation variable volume word writer



