### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [Help] Decimal into binary system  (Read 10107 times)

• Newbie
• Posts: 2
##### [Help] Decimal into binary system
« on: February 07, 2017, 07:18:56 pm »

I need to write code for this task:

Write a program which converts an integer from decimal into binary system, and writes it in direct, one's complement and two's complement notations. Modify the program to perform summation.

#### Bart

• Hero Member
• Posts: 3912
##### Re: [Help] Decimal into binary system
« Reply #1 on: February 07, 2017, 07:21:17 pm »
And what have you got so far?

Bart

• Newbie
• Posts: 2
##### Re: [Help] Decimal into binary system
« Reply #2 on: February 07, 2017, 07:38:28 pm »
My friend need to do this. I'm not a programmer. I didn't know where to turn.

#### Bart

• Hero Member
• Posts: 3912
##### Re: [Help] Decimal into binary system
« Reply #3 on: February 08, 2017, 12:34:14 am »
Yeah, right.
Best one so far.

Bart

#### Lupp

• New Member
• Posts: 31
##### Re: [Help] Decimal into binary system
« Reply #4 on: February 08, 2017, 12:59:35 am »
There is a function IntToBin in the unit StrUtils that can give you the exact bit pattern of the memory representation of a number of an integer type as a string. The rest is arithmetics and string manipulation as usual.

#### Bart

• Hero Member
• Posts: 3912
##### Re: [Help] Decimal into binary system
« Reply #5 on: February 08, 2017, 12:52:52 pm »
There is a function IntToBin in the unit StrUtils that can give you ...

No, it can give his friend the exact bitpattern ....

Bart

#### Handoko

• Hero Member
• Posts: 3765
• My goal: build my own game engine using Lazarus
##### Re: [Help] Decimal into binary system
« Reply #6 on: February 08, 2017, 12:58:20 pm »
https://en.wikipedia.org/wiki/Ones%27_complement
https://en.wikipedia.org/wiki/Two%27s_complement

#### Lupp

• New Member
• Posts: 31
##### Re: [Help] Decimal into binary system
« Reply #7 on: February 08, 2017, 01:07:32 pm »
And ...
Attached an example for anybody and their friends.

#27 means "The key read as ASCII control character number 27." That's the 'Esc' key.
« Last Edit: February 08, 2017, 01:16:16 pm by Lupp »

#### Handoko

• Hero Member
• Posts: 3765
• My goal: build my own game engine using Lazarus
##### Re: [Help] Decimal into binary system
« Reply #8 on: February 08, 2017, 01:11:36 pm »
Unit Crt can't run properly on my machine (don't know why). So I copy/pasted Lupp's code here for others to try and inspect.

Code: Pascal  [Select][+][-]
1. program Dec2Bin;
2. uses
3.   Crt, StrUtils;
4. type
5.   MyType = Longint; //e.g.
6. var
7.   MyNumber : MyType;
8.
9. var
10.   fNum: Double;
11.   BytesOf: array[1..8] of Byte absolute fNum;
12.   BinStr: String;
13.   j: Integer;
14. begin
15.   ClrScr;
16.   Writeln('First some integers (may be Longint).');
17.   repeat
19.     WriteLn('That''s in binary: ':30, IntToBin(MyNumber, SizeOf(MyType)*8));
20.     WriteLn('And the direct complement: ':30, IntToBin(NOT MyNumber, SizeOf(MyType)*8));
21.     WriteLn('Or whatever you want: ':30, IntToBin(- MyNumber, SizeOf(MyType)*8));
23.
24.   Writeln('And now numbers with a decimal part and probably in scientific notation.');
25.   WriteLn('No complements in this case.');
26.   ClrScr;
27.   repeat
29.     BinStr := '';
30.     for j := 8 downto 1 do BinStr := BinStr + IntToBin(BytesOf[j],8);
31.     WriteLn('That''s in binary standard 64-bit Double format: ');
32.     WriteLn(BinStr);
33.
35.
36. end.

#### Bart

• Hero Member
• Posts: 3912
##### Re: [Help] Decimal into binary system
« Reply #9 on: February 08, 2017, 01:56:42 pm »
OK, now without any effort at all for TS we solved his friends homework assignment.

Personally I object to that.

Bart

#### wp

• Hero Member
• Posts: 7551
##### Re: [Help] Decimal into binary system
« Reply #10 on: February 08, 2017, 02:13:58 pm »
Well I think application of a ready-made function certainly has not been in the intention of the teacher who gave this assignment. The OP will have a problem showing the posted solution anyway.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

#### Leledumbo

• Hero Member
• Posts: 8266
• Programming + Glam Metal + Tae Kwon Do = Me
##### Re: [Help] Decimal into binary system
« Reply #11 on: February 08, 2017, 02:18:50 pm »
OK, now without any effort at all for TS we solved his friends homework assignment.

Personally I object to that.

Bart
Relax, the teacher will reject the use of such built-in function. Plus, IntToBin only gives answer in one's complement. He will still need hammer his head to get the two's complement (despite it's very trivial).

#### Handoko

• Hero Member
• Posts: 3765
• My goal: build my own game engine using Lazarus
##### Re: [Help] Decimal into binary system
« Reply #12 on: February 08, 2017, 02:32:23 pm »
It is sad 'his friend' cannot visit here, otherwise we can have better communication and guide him/her better. Novices often learn nothing if receiving working code directly for assignment.

#### Bart

• Hero Member
• Posts: 3912
##### Re: [Help] Decimal into binary system
« Reply #13 on: February 08, 2017, 03:37:27 pm »
Relax, the teacher will reject the use of such built-in function.

His friend only has to copy the implementation of said function (for 1 implmentation).
Of course this implementation may still be way above the skills he is expected to have, so his friends teacher might still reject it.

I still think however that we should help people who ask for this, but (especially for homework assignments) we should not give away code, but give hints and tips (and possibly minor corrections) on the code they can show us.

It's a matter of principle.

Bart

#### Handoko

• Hero Member
• Posts: 3765
• My goal: build my own game engine using Lazarus
##### Re: [Help] Decimal into binary system
« Reply #14 on: February 08, 2017, 03:42:18 pm »
I still think however that we should help people who ask for this, but (especially for homework assignments) we should not give away code, but give hints and tips (and possibly minor corrections) on the code they can show us.

Very agree. I give hints, rarely codes.