2's complement in BASIC

Basic, Programming, 2's, complement

Page 1 of 1

4 Replies - 3388 Views - Last Post: 09 January 2011 - 10:42 PM Rate Topic: -----

#1 Ashley.O  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 29-November 10

2's complement in BASIC

Posted 29 November 2010 - 07:22 PM

Hello - I am a liberal arts major taking a introduction to computers class and am really struggling to write a program in BASIC that takes a whole number input and converts it to 2's complement binary.

My questions are as follows:

1. can i write a BASIC program in the Terminal on a Mac?
2. I know i'm not supposed to ask for code, but a little direction (!) would help!

Thanks!!!!!!!
Is This A Good Question/Topic? 0
  • +

Replies To: 2's complement in BASIC

#2 calvinthedestroyer  Icon User is offline

  • D.I.C Lover

Reputation: 167
  • View blog
  • Posts: 1,908
  • Joined: 13-October 07

Re: 2's complement in BASIC

Posted 01 December 2010 - 11:42 PM

What type of BASIC are you using in class?

You should be able to write your code in a text editor and then save the files with what ever extension that you need.

I don't use a Mac so I don't know what text editors they use.

As far as how to do 2's complement, I would look to wikipedia.org 2s_complement
Was This Post Helpful? 0
  • +
  • -

#3 Ashley.O  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 6
  • Joined: 29-November 10

Re: 2's complement in BASIC

Posted 02 December 2010 - 04:26 PM

Here is what I have:
10 DIM B(15) 
20 INPUT "Enter a number: ", N 
30 IF N >= 32768 THEN GOTO 1000 
40 IF N < -32768 THEN GOTO 1020 
50 REM IF N = "q" STOP 
60 PRINT "The 16 bit two's compliment binary representation of "; N" is:"
70 J = 0 
80 K = 1 
90 IF N >= 0 THEN GOTO 140 
100 B(15) = 1 
110 N = ABS(N+1) 
120 J = 1 
130 K = 0 
140 FOR I = 0 TO 14 
150 B(I) = J 
160 IF N = 0 THEN NEXT I 
170 IF N MOD 2 = 1 THEN B(I) = K 
180 N = INT(N/2) 
190 NEXT I 
200 FOR I = 14 to 0 STEP -1 
210 PRINT B(I); 
220 NEXT I 
230 GOTO 20 

1000 PRINT "That number is larger than 16 bits; try a lower number." 
1010 GOTO 10 
1020 PRINT "That number is larger than 16 bits; try a higher number." 
1030 GOTO 10




Lines 50 and 60 may have issues and there should be a line 85 where B(15) = 0... delete line 100 shift 140 to 145 and make a new 140 that reads B(15) = J ??

Any suggestions?

This post has been edited by macosxnerd101: 02 December 2010 - 04:27 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: 2's complement in BASIC

Posted 03 December 2010 - 07:25 AM

Cool. Ancient BASIC with line numbers? I think I just had a flashback! :P

Your print is buggered. This should work:
60 PRINT "The 16 bit two's compliment binary representation of ", N, " is:"



Line 160 gives me an error. Perhaps put the stuff in your for loop in a gosub?
Was This Post Helpful? 1
  • +
  • -

#5 calvinthedestroyer  Icon User is offline

  • D.I.C Lover

Reputation: 167
  • View blog
  • Posts: 1,908
  • Joined: 13-October 07

Re: 2's complement in BASIC

Posted 09 January 2011 - 10:42 PM

60 PRINT "The 16 bit two's compliment binary representation of "; N;" is:"

Don't you have to have a ; after a variable as well?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1