Code Monkey home page Code Monkey logo

hs-ruby-binary-stretch-lab's Introduction

###Getting Down to Business with Binary This is a group lab, so make sure you are working in a group of 2-4 students

To start this lab, let's count to ten. Ok, fine, any 3 year old could count to ten. But can you count to ten in binary? How about in hexadecimal? Not so easy...yet!

The number system we use is called the decimal number system. We say it is base 10 because it uses 10 digits from 0 to 9. But what happens when we count past 9? We don't have a single symbol for 10, so you have to add 1 in a new column and then start over again at 0 in the ones place.

0, 1, 2, 3, 4, 5, 6, 7, 8, 9...now start over by adding a new column (the tens place) worth ten times the first column (the ones place) 10, 11, 12...

Other systems use different bases. Binary, for example, is base 2. Because of this, we only have two digits we count with: 0 and 1. Let's see how that works. You count 0, 1, then you have to start over at zero and add a column! The next column is worth twice the value of the first column. Since binary is a base 2 system, each digit represents a power of 2, with the rightmost digit representing 20 (0), the next representing 21 (2), then 22 (4), 23 (8) and so on.

Decimal | Binary | Explanation

  • | ------ | ----------- 0 | 0 | Start at zero 1 | 1 | Then 1 2 | 10 | Start back at zero, add 1 to the left 3 | 11 | 4 | 100 | Start all numbers back at zero again, add one to the left again 5 | 101 | One in the fours place, one in the ones place 6 | 110 | One in the fours place, one in the twos place 7 | 111 | One in the fours place, one in the twos place, one in the ones place 8 | 1000 | One in the eights place

Your Challenge

  • Create a method called decimal_to_binary that converts a decimal number to its binary equivalent. Your method should take in a decimal as an argument and output it as binary.

  • Create a method called binary_to_decimal that converts a binary number to its decimal equivalent. Your program should take in a binary number and output it as a decimal.

Write your code in the binary.rb file. Run learn to see if your code passes the tests written for it. You should be able to successfully convert the following decimal numbers to binary and binary numbers to decimal:

Decimal | Binary

  • | ------ 1 | 1 2 | 10 13 | 1101 34 | 100010 100 | 1100100 526 | 1000001110

Bonus Challenge

Google the hexadecimal number system. It works just like binary or decimal, but has a base of 16. Once you've figured out how hexadecimals work, create a program that converts a decimal number to its hexadecimal equivalent, and vice versa (as above). Write your code in the hexadecimal.rb file.

View Getting Down to Business with Binary on Learn.co and start learning to code for free.

hs-ruby-binary-stretch-lab's People

Contributors

dfenjves avatar fislabstest avatar fs-lms-test-bot avatar juliataitz avatar stephaniecoleman avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

max1435

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.