### V22.0101 Fall, 2007

{updated on 9/18}

Binary Representation of Numbers

### ASSIGNMENT # 1: Section 1 Due: Tuesday, 9/25/07 By midnight

Binary Representation of Numbers:
Decimal numbers from 0 through 255 can be represented in the binary number system using 8 bits. Here are some examples

DecimalBinary
0 00000000
1 00000001
2 00000010
3 00000011
7 00000111
15 00001111
16 00010000
25 00011001
128 10000000
129 10000001
254 11111110
255 11111111

See details for conversions from decimal to binary and binary to decimal from this URL: http://www.danbbs.dk/~erikoest/binary.htm#top

• Write a Java class/program that will receive a decimal number between 0 and 255 and then outputs its binary equivalent. It should also convert back the binary number to its decimal equivalent for verification.
• Your program should ask the user to input a decimal number between 0 and 255. If it is not, it should print an informative error message (using System.out.println).
• You can use the Scanner or the JOption class to input a decimal number between 0 and 255.
• {updated on 9/18}
• Your program should use the follwoing methods:
• First Method: Should convert the decimal number that the user inputted from above to its binary equivalent.
• Second Method: (to verify conversion) Should convert the binary number that was converted in the above method to its decimal equivalent.
• (see details on this website for conversions from decimal to binary and binary to decimal: http://www.danbbs.dk/~erikoest/binary.htm#top )
• The easiest is to generate the binary bits from LEFT to RIGHT:
• You can use a single for loop.
• This loop generates the 8 bits, one at a time.There are two possible ways to do this. The easiest is to generate the bits from LEFT to RIGHT, by successively subtracting powers of 2 from the number inputted.
• Take the number 25 for example. If we subtract the biggest relevant power of 2, the number 128, from this, we get a negative answer so we know the first bit must be 0. The first power of 2 which is smaller than 25 is 16, so that tells us that the bit corresponding to that power (that's actually the fourth bit from the left) is 1. Once 16 is taken away from 25, we have 9 left. The next power of 2 is 8, which is less than 9, so the next bit (the fifth from the left) is also 1. Now we only have 1 left, so the sixth and seventh bits are 0, and the final eights bit is 1. You should not type all the powers of 2 into the program. You should start with 128, and then repeatedly divide this by 2 in the loop. Don't use any of the numbers in the table above for an example. Choose another number, say between 70 and 250 for your example. Don't use the same example as a classmate, if you can avoid it. (Of course, some people may choose the same example by accident -that's OK.)
• There's an alternative way to code this (generating the bits from RIGHT to LEFT)
• For example, the decimal number 25 is represented in binary as 00011001, meaning 0 times 128 plus 0 times 64 plus 0 times 32 plus 1 times 16 plus 1 times 8 plus 0 times 4 plus 0 times 2 plus 1 times 1.
• Notice that EACH BIT CORRESPONDS TO A POWER OF 2.
• Make sure you understand how this works before you go any further.

General Notes:

• Please read the assignment carefully. If you need help, don't hesitate to contact your E-TUTOR, but send the email EARLY, NOT just before the deadline. You can also see the TA, email the etutor or the Professor during their office hours.
• Even though this is the first assignment, it uses concepts from all of Chapters 2 through 3.
• Please get started on the assignment immediately, and contact your etutor as soon as possible with any questions. Don't expect a response the day before the due date.

#### Extra credit:

1. Provide a pseudocode code for your program
• Helpful resources on how to write pseudocode:
2. Provide flowchart for your program
• Helpful resources on how to design flowchart:

#### PART TWO:

• When you are done and have tested it, email your source file "Classname.java" as an attachment to the course grader account at f7010103 (AT) cs (DOT) nyu (DOT) edu