| | |
Exercise using: unsigned char bcd(int n);
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
>Not totally I hope.
That was my intention yes, but then again, I do like to try and learn this, it's just so frustrating that even when getting help from you and Dave, I STILL can't figure it out :!:
> When I try this with the value 12345, I get this as a binary code: 10011100000011
Tough, the code should be 0001 0010 0011 0100 0101
>12345 -> 0001 0010 0011 0100 0101
I understand that the decimal value equals this binary code, the problem is, how in the h*ll do I get (0100 0101) into a return value (unsigned char) :mad:
That was my intention yes, but then again, I do like to try and learn this, it's just so frustrating that even when getting help from you and Dave, I STILL can't figure it out :!:
>
C Syntax (Toggle Plain Text)
while ( value != 0 ) { cout<< !!( value & 1 ); value >>= 1; }
Tough, the code should be 0001 0010 0011 0100 0101
>12345 -> 0001 0010 0011 0100 0101
I understand that the decimal value equals this binary code, the problem is, how in the h*ll do I get (0100 0101) into a return value (unsigned char) :mad:
•
•
•
•
Originally Posted by JoBe
>12345 -> 0001 0010 0011 0100 0101
I understand that the decimal value equals this binary code
Perhaps some debugging printfs can help.
#include <stdio.h>
unsigned int dec2bcd(unsigned int dec)
{
unsigned int bits, bcd = 0;
for ( bits = 0; dec; bits += 4, dec /= 10 )
{
printf("dec = %5u, bits = %2u, bcd = %6u (0x%04x)\n", dec, bits, bcd, bcd);
bcd |= dec % 10 << bits;
}
return bcd;
}
int main()
{
unsigned int value = 12345, result = dec2bcd(value);
printf("bcd(%u) = 0x%X [%u]\n", value, result, result);
return 0;
}
/* my output
dec = 12345, bits = 0, bcd = 0 (0x0000)
dec = 1234, bits = 4, bcd = 5 (0x0005)
dec = 123, bits = 8, bcd = 69 (0x0045)
dec = 12, bits = 12, bcd = 837 (0x0345)
dec = 1, bits = 16, bcd = 9029 (0x2345)
bcd(12345) = 0x12345 [74565]
*/ "One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
>I STILL can't figure it out
Binary is a bitch to understand at first. This has nothing to do with your ability, it's a difficult subject for anyone to wrap their mind around when they first start working with.
>When I try this with the value 12345, I get this as a binary code: 10011100000011
That loop only prints the bits of a value. Unless 12345 has already been binary coded, you should expect 11000000111001, which because the loop prints the bits in reverse order, gives you 10011100000011. You're still confusing things:
12 = 1100
This is a binary value that is directly equivalent to the integral value 12. Notice how the value 12 fits in 4 bits. My loop prints this.
12 = 00010010
This is a binary encoding of the two integral digits 1 and 2. Notice how there's no way to fit this encoding in 4 bits, so 8 bits are used. Your bcd function returns this.
>how in the h*ll do I get (0100 0101) into a return value (unsigned char)
Return the integral value 69. 69 has the bit pattern 01000101, which is what you want and why I said this is a bit packing exercise.
Binary is a bitch to understand at first. This has nothing to do with your ability, it's a difficult subject for anyone to wrap their mind around when they first start working with.
>When I try this with the value 12345, I get this as a binary code: 10011100000011
That loop only prints the bits of a value. Unless 12345 has already been binary coded, you should expect 11000000111001, which because the loop prints the bits in reverse order, gives you 10011100000011. You're still confusing things:
12 = 1100
This is a binary value that is directly equivalent to the integral value 12. Notice how the value 12 fits in 4 bits. My loop prints this.
12 = 00010010
This is a binary encoding of the two integral digits 1 and 2. Notice how there's no way to fit this encoding in 4 bits, so 8 bits are used. Your bcd function returns this.
>how in the h*ll do I get (0100 0101) into a return value (unsigned char)
Return the integral value 69. 69 has the bit pattern 01000101, which is what you want and why I said this is a bit packing exercise.
I'm here to prove you wrong.
Well, after the additional tips you gave, I got to the point that I'm getting an output of 1010001, apparantly, I'm still missing one 0 at the back :-|
THe code I used was this:
So, all I have to do know is reverse the value 1010001 into '0'100 0101, correct
Also, could you explain how this piece of code works:
>12 = 1100
I understand this Narue, because four bits can have a maxim value of 15, wich also equals the amount in hex from 0 to 9 and A to F.
>12 = 00010010
This however I don't understand
I mean, I understand it to be eight bits
@ Dave,
Thanks for the additional info, however I didn't try out the code you wrote because I feared I would get even more confuzed then I allready am :!:
I did include the link you gave me into my favorites and will read the article later on, thanks for that :!:
THe code I used was this:
C Syntax (Toggle Plain Text)
int main() { int value = 12345; char x; x = bcd(value); while ( x != 0 ) { cout<< !!( x & 1 ); x >>= 1; } return 0; } unsigned char bcd (int n) { if ( n >= 10 ) { int a = n % 10; int b = ( n / 10 ) % 10; return ( b << 4 ) | a; } return n; }
So, all I have to do know is reverse the value 1010001 into '0'100 0101, correct
Also, could you explain how this piece of code works:
C Syntax (Toggle Plain Text)
!!( x & 1 )
>12 = 1100
I understand this Narue, because four bits can have a maxim value of 15, wich also equals the amount in hex from 0 to 9 and A to F.
>12 = 00010010
This however I don't understand
I mean, I understand it to be eight bits@ Dave,
Thanks for the additional info, however I didn't try out the code you wrote because I feared I would get even more confuzed then I allready am :!:
I did include the link you gave me into my favorites and will read the article later on, thanks for that :!:
>Also, could you explain how this piece of code works
x & 1 lets you access the low order bit of x. The double bang trick guarantees that the resulting value is normalized to 0 or 1. Figuring it out is a fun exercise, try taking away one !, then take away the other. Try using something other than 1 for the right hand side of the & and do the same thing.
>I mean, I understand it to be eight bits
Then you understand without understanding it seems.
Yes, it is eight bits, because you're taking each digit of the original number (12, or 1 and 2, or 0001 and 0010) setting aside four bits for each of them, and then pasting those four bit chunks together to get the binary code, 00010010.
x & 1 lets you access the low order bit of x. The double bang trick guarantees that the resulting value is normalized to 0 or 1. Figuring it out is a fun exercise, try taking away one !, then take away the other. Try using something other than 1 for the right hand side of the & and do the same thing.
>I mean, I understand it to be eight bits
Then you understand without understanding it seems.
Yes, it is eight bits, because you're taking each digit of the original number (12, or 1 and 2, or 0001 and 0010) setting aside four bits for each of them, and then pasting those four bit chunks together to get the binary code, 00010010. I'm here to prove you wrong.
Nope, not working :!:
When I use this code:
My result is this binary code: 01001 :o When it should be 0001 0010 :rolleyes:
I tried out using I tried out using I tried out using I tried out ... several other changes in this piece of code, they all gave different outcomes, but to be honest, I don't know WHY it happend and I don't know why or when I would use these changes because I don't know what happens with the code
I also changed this into this wich actually returned this: 100001
Why, I have no idea
So, to make it short, after all tries and tribulations
I still haven't found the solution to my exercise :!:
DARN
When I use this code:
C Syntax (Toggle Plain Text)
#include <iostream> #include <sstream> #include <string> using namespace std; unsigned char bcd (int n); int main() { int value = 12; char x; x = bcd(value); while ( x != 0 ) { cout<< !!( x & 1 ); x >>= 1; } return 0; } unsigned char bcd (int n) { if ( n >= 10 ) { int a = n % 10; int b = ( n / 10 ) % 10; return ( b << 4 ) | a; } return n; }
My result is this binary code: 01001 :o When it should be 0001 0010 :rolleyes:
I tried out using
C Syntax (Toggle Plain Text)
!( x & 1 );
C Syntax (Toggle Plain Text)
( x & 1 );
C Syntax (Toggle Plain Text)
!!( x & 2 );

I also changed this
C Syntax (Toggle Plain Text)
return ( b << 4 ) | c;
C Syntax (Toggle Plain Text)
return ( a << 4 ) | b;
Why, I have no idea
So, to make it short, after all tries and tribulations
I still haven't found the solution to my exercise :!: DARN
>Nope, not working
It's working fine, just not the way you want it to.
>My result is this binary code: 01001 When it should be 0001 0010
Okay, reverse what you got because the loop prints the low-order bits first:
10010
Now notice the condition for the printing loop, while x != 0. Because the loop only prints as long as there's a 1 bit somewhere in the value, leading 0's are ignored. So you're getting the correct output, it's just not formatted as you expected.
To be perfectly honest, that method of printing bits only has the advantage of being simple to write correctly the first time. Another easy (if incredibly obscure) way to print the bits properly is with bitset:
You can do it manually without the aid of the standard library like so:
But for the usual reasons, that's not as good of a solution.
It's working fine, just not the way you want it to.
>My result is this binary code: 01001 When it should be 0001 0010
Okay, reverse what you got because the loop prints the low-order bits first:
10010
Now notice the condition for the printing loop, while x != 0. Because the loop only prints as long as there's a 1 bit somewhere in the value, leading 0's are ignored. So you're getting the correct output, it's just not formatted as you expected.
To be perfectly honest, that method of printing bits only has the advantage of being simple to write correctly the first time. Another easy (if incredibly obscure) way to print the bits properly is with bitset:
C Syntax (Toggle Plain Text)
#include <bitset> #include <iostream> using namespace std; unsigned char bcd (int n); int main() { int value = 12345; unsigned char x; x = bcd(value); cout<< bitset<8> ( x ).to_string<char, char_traits<char>, allocator<char> >() <<endl; return 0; } unsigned char bcd (int n) { if ( n >= 10 ) { int a = n % 10; int b = ( n / 10 ) % 10; return ( b << 4 ) | a; } return n; }
C Syntax (Toggle Plain Text)
#include <bitset> #include <iostream> using namespace std; unsigned char bcd (int n); int main() { int value = 12345; unsigned char x; x = bcd(value); for ( int i = 7; i >= 0; i-- ) cout<< !!( x & ( 1U << i ) ); return 0; } unsigned char bcd (int n) { if ( n >= 10 ) { int a = n % 10; int b = ( n / 10 ) % 10; return ( b << 4 ) | a; } return n; }
I'm here to prove you wrong.
[HOMER] DOH :!: [/HOMER]
Oh Narue, why do you allways make it look so simple :mrgreen:
>
I understand '!!' being used to make sure that only 0 or 1 are shown.
Like in the previous thread, you said that x & 1 was used for accessing the lower bit, but why is there U mentioned behind it
>But for the usual reasons, that's not as good of a solution.
Why is the other solution better then
Anyway, thanks for the help
Oh Narue, why do you allways make it look so simple :mrgreen:
>
C Syntax (Toggle Plain Text)
cout<< !!( x & ( 1U << i ) );
I understand '!!' being used to make sure that only 0 or 1 are shown.
Like in the previous thread, you said that x & 1 was used for accessing the lower bit, but why is there U mentioned behind it
>But for the usual reasons, that's not as good of a solution.
Why is the other solution better then
Anyway, thanks for the help
>Oh Narue, why do you allways make it look so simple
As you gain experience, hard things become simple and impossible things become hard.
>why is there U mentioned behind it
Force of habit. It's always a good idea to force all parts of a bitwise expression to unsigned types because the bitwise operators are surprisingly tricky to use with signed types. 1U basically says that 1 is an unsigned int rather than a signed int.
>Why is the other solution better then
The usual suspects:
1) Portability (the same code will work the same everywhere)
2) Reuse (you don't have to solve the same problems over and over)
3) Performance (the standard library is written to be fast)
As you gain experience, hard things become simple and impossible things become hard.

>why is there U mentioned behind it
Force of habit. It's always a good idea to force all parts of a bitwise expression to unsigned types because the bitwise operators are surprisingly tricky to use with signed types. 1U basically says that 1 is an unsigned int rather than a signed int.
>Why is the other solution better then
The usual suspects:
1) Portability (the same code will work the same everywhere)
2) Reuse (you don't have to solve the same problems over and over)
3) Performance (the standard library is written to be fast)
I'm here to prove you wrong.
![]() |
Other Threads in the C Forum
- Previous Thread: infinite loop
- Next Thread: Combinations(N choose R) ?
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h






