Tutorial4: Data Representation / Numbering Conversion / File Permissions

From CDOT Wiki
Revision as of 17:49, 20 January 2020 by Msaul (talk | contribs) (LINUX PRACTICE QUESTIONS)
Jump to: navigation, search

Data Representation / Numbering Conversion / File Permissions

Main Objectives of this Practice Tutorial

  • Understand the importance of how computers store data (i.e. data representation)
  • Define decimal, binary, octal and hexadecimal numbers
  • Perform various numbering conversions between the decimal, binary, octal and hexadecimal numbering systems
    by hand without the use of a computer or calculator
  • Identify which numbering system conversion method to use when required to perform a numbering conversion
  • Understand directory and regular file permissions
  • Learn how to set directory and regular file permissions with the chmod command (symbolic and octal methods)
  • Learn how to use the umask command to have permissions for directories and files automatically set upon their creation

Tutorial Reference Material

Course Notes
Numbering Conversion / File Permissions Reference
YouTube Videos
Slides:


Data Representation Definitions File Permission Concepts

File Permission Commands

Instructional Videos:

KEY CONCEPTS

Why Study Data Representation?

A series of binary numbers form a byte to represent numbers.
(Image licensed under cc)
Bytes can be used to also represent characters. It is job of a program to know if bytes are used to represent numbers or characters. Learning to convert numbering systems
(like Hexadecimal to Binary) can be used to know how a character is represented in binary.
(Image licensed under cc)

Data ... is any sequence of one or more symbols given meaning by specific act(s) of interpretation. Digital data is data that is represented using the binary number system of ones (1) and zeros (0)...
Reference: https://en.wikipedia.org/wiki/Data_(computing)

Binary numbers are grouped together to form a byte. Bytes are used to represent numbers or characters. Programmers create programs to interpret those series of binary numbers as numbers or characters. It is important to learn how to convert data down to the level of the computer (binary).

Reasons to Understand Data Representation:

  • C Programming: Sending information over networks, files
  • Web Development: Setting color codes for webpage background or text
  • Allowing or Limiting Unix / Linux File Access: Setting permissions for files and directories


In this tutorial, you will learn how a simple decimal number (integer) is stored into the computer system as a binary number. We will also learn other numbering systems (octal and hexadecimal) that can be used as a "short-cut" to represent binary numbers.

Decimal / Binary / Octal / Hexadecimal Numbering Systems

The decimal numbering system .
(Image licensed under cc - modified by author).

Decimal Numbers

The decimal numbering system consists of digits consisting of numbers 0 to 9. The fact that humans started counting on their fingers and thumbs most likely lead to the development of this numbering system.

The numbering system is based on sums of the power of 10 which provides rules for mathematic calculations.

Referring to the diagram to the right, the value of each decimal digit consists of the value (placeholder) multiplied by the corresponding power of 10. For example, units are 100, tens are 101, hundred are 102 which move in a right-to-left direction.

The binary numbering system.
(Image licensed under cc)

Binary Numbers

The binary numbering system consists of digits consisting of numbers 0 or 1. Digital computers have circuits which representing data in terms of voltage levels. Multiple circuits are used to represent data (in the form of binary numbers).

The numbering system is based on sums of the power of 2.

Referring to the diagram to the right, the value of each decimal digit consists of the value (placeholder) multiplied by the corresponding power of 2. For example, 20 , 21, 22, etc. which move in a right-to-left direction.

Octal / Hexadecimal Numbers

The octal numbering system.
(Image licensed under cc)
The hexadecimal numbering system.
(Image licensed under cc)

The octal and hexadecimal numbering systems consist of digits of numbers 0 to 7 and 0 to F respectively. For hexadecimal numbers, values for 10 to 15 are represented by the characters A to F respectively.

The octal and hexadecimal numbering system are based on sums of the power of 8 and 16 respectively.

Numbering Conversion Methods

Method 1: Binary to Decimal

Performing a binary to decimal conversion.

When converting binary numbers to decimal numbers, perform the following steps:

  1. Write the binary number.
  2. Starting from the right-side, draw L's moving to the left (refer to diagram on right).
  3. Starting on the right, multiply the number by 2 to the power of zero.
  4. Repeat moving to the left, but increase the power by 1 each time.
  5. Add up the results to obtain the decimal value equivalent.



NOTE: To convert other numbering system to decimal, replace the number 2
(in red in the diagram to the right) with 8 (for octal) or 16 (for hexadecimal)
.

Method 2: Decimal to Binary

Performing a decimal to binary conversion.

When converting decimal numbers to binary numbers, perform the following steps:

  1. On the left side, write the decimal number to be converted.
  2. Far to the right, write the number 1 and while moving leftwards, double the number until that number is NOT greater than the decimal number to be converted (refer to the diagram).
  3. If you are converting to 8-bit, 32-bit, etc., enter leading zeros if those doubled numbers are greater than the decimal number.
  4. Moving in a rightwards direction, if the doubled number less than the decimal number, write a 1 and subtract the double number's value from the decimal number.
  5. If the next doubled number is greater then the remainder, then write a zero; otherwise, if the number is less than but not zero, repeat the above steps #4 and #5 until you have obtained your binary number.



Method 3: Octal to Binary / Binary to Octal

Performing an binary to octal numbering conversion.
Performing an octal to binary numbering conversion.

Binary to Octal

  1. One octal number represents 3 binary numbers, so starting from right-side, group binary digits into groups of 3
    (add leading zeros if necessary).
  2. Write (4)(2)(1) under each group of 3 binary numbers.
  3. Multiply the placeholders (i.e. 0's and 1's) by the corresponding (4)(2)(1) for each group to obtain the octal number (refer to diagram of binary to octal conversion).

Octal to Binary

  1. One octal number represents 3 binary numbers, so space-out
    the octal numbers to make space for a binary number.
  2. Write (4)(2)(1) under each octal number.
  3. Write 0's or 1's for each group of binary numbers to add up to the
    corresponding octal number (refer to diagram of octal to binary conversion).

Method 4: Hexadecimal to Binary / Binary to Hexadecimal

Performing a binary to hexadecimal conversion.
Performing a hexadecimal to binary conversion.

Binary to Hexadecimal

  1. One hexadecimal number represents 4 binary numbers, so starting from right-side, group binary digits into groups of 4 (add leading zeros if necessary).
  2. Write (8)(4)(2)(1) under each group of 3 binary numbers.
  3. Multiply the placeholders (i.e. 0's and 1's) by the corresponding (8)(4)(2)(1) for each group to obtain the octal number.
  4. Convert values from 10 to 15 to A to F
    (refer to diagram of binary to hexadecimal conversion)

Hexadecimal to Binary

  1. One hexadecimal number represents 4 binary numbers,
    so space-out the hexadecimal numbers to make space for a binary number.
  2. Convert letters A to F to 10 to 15 (refer to diagram of binary to hexadecimal conversion)
  3. Write (8)(4)(2)(1) under each hexadecimal number.
  4. Write 0's or 1's for each group of binary numbers to add up to the corresponding
    hexadecimal number (refer to diagram of hexadecimal to binary conversion).

File Permissions

Since the Unix / Linux operating systems allow for multiple users to be created on a single server,
it is essential for users on these servers to share or limit access to directories and files contained in those directories.

Detailed directory listing showing permissions for a directory and a regular file.

When directories and regular files are created, they are assigned to an owner
(typically the username which is the creator). To allow or limit access to those files and directories, those files and directories are assigned to an initial group referred to as a "primary group".

Users that own those directories and files are referred to as users, users that belong within the same group are referred to as same group members, and those users are do NOT belong to a
particular group are referred to as other group members.

NOTE: In this course, we cannot create groups or assign users to groups - you may learn how to do
that if you take a Unix/Linux administration account. On the other hand, you can change which user,
existing same group members or existing other group members can or cannot access a directory or regular file.

Permissions of a directory that contain subdirectories and regular files.
Permissions of a regular file contained within a directory.

File Permissions consist of two-layers:

First, the permissions of a directory that contains regular files, and second, permissions of the regular files themselves.


Permissions for Directory files have a different meaning than permissions for regular files:


  • Referring to the left-most diagram above, read (r) permissions permit viewing directory contents, write (w) permissions allow subdirectories and regular files to be created in that directory, and execute (x) permissions permit access to that directory. These permissions relate to the 3 categories: user (owner), same group members, other group members.
  • Referring to the rightmost diagram above, read (r) permissions permit viewing regular file contents, write (w) permissions allow file contents to be modified/edited, and execute (x) permissions permit the file to be run (e.g. running a program or shell script). Again, these permissions relate to the 3 categories: user (owner), same group members, other group members.


NOTE: The symbol dash "-" indicates that the permission is NOT granted.

The permissions of newly-created directories and regular files are automatically assigned via a user mask (we will discuss this shortly). In order to change permissions for directories and regular files, you would use the chmod command.


Changing File Permissions with 'chmod' command:

Symbolic Method:

The chmod can use symbols to add, remove, and set rwx permissions for user,
same group members, other group members or ALL categories:

CommandDescription
chmod ugo+x script.bashAdd execute permissions to the file script.bash so it can be run.
chmod u=rwx,go=x ~Set "pass-thru" permissions of your home directory for same group members and other group members to navigate to other subdirectories (that may have access / view permissions).
chmod go-w ~/sharedRemove write permissions for same group members and other group members for the directory ~/shared
chmod a=rx mySet read and execute permissions for the directory myfile.txt


Octal (Absolute) Method:

using octal numbers to represent setting file permissions.

You can use octal numbers to represent permissions.

Since 1 octal digit represents 3 binary digits, one octal digit can represent the rwx permission granted or NOT granted. The permissions rwz would be in the form of 3 binary digits (1 represents the permission granted and 0 represents the permission NOT granted.

NOTE: You can only use this method to set file permissions (as opposed to add or remove permissions).

CommandDescription
chmod 500 script.bashSet read and execute permissions for only the user for the file script.bash so it can be run.
chmod 711 ~Set "pass-thru" permissions of your home directory for same group members and other group members to navigate to other subdirectories (that may have access / view permissions).
chmod 750 ~/sharedSet full permissions for user, read and access permissions for some group members and no permissions for other group members for the directory ~/shared
chmod 555 myfile.txtSet read and execute permissions for the directory myfile.txt

NOTE: You can use the -R option to set permissions for directory, subdirectory and directory contents recursively.


Setting umask for newly-created directories.
Setting umask for newly-created regular files

Setting Permissions for Newly-Created Directories and Regular Files (umask):

The umask command is used to set the permissions of newly-created directories and regular files.


Issuing the umask command without arguments will display the current umask value. Refer to the diagrams on the right-side to set the umask value for directories and regular files. Setting the umask value (for example umask 022) only takes effect for the current shell session unless the umask command is contained in a start-up file
(e.g. .profile, .bash_profile, or .bashrc).

INVESTIGATION 1: NUMBERING CONVERSIONS

For this investigation, we will NOT be logged into our Matrix account, but it is recommended to have sheets of paper ready to manually perform numbering conversations.

NOTE: It is essential that you learn how to manually perform numbering conversions since you will NOT be permitted to perform quizzes, midterm, or your final exam with a computer or a calculator. Learning to quickly perform manual numbering conversions will may IT professional more productive such as setting permissions, designing computer networks, or selecting complex colors when developing webpages.

Only use a calculator to check your numbering conversion AFTER you have performed the operation manually.

You will now get practice performing numbering conversions.

Perform the Following Steps:

  1. Let's convert the following 8-bit binary number 10111110 to a decimal number.

    NOTE: It is extremely important to learn and memorize the correct techniques to perform the
    proper numbering conversion method (i.e. view the method above (drawing the L's).


  2. Write the manual conversion on a sheet of paper.

  3. Use a calculator to check your work. In MS Windows, you can set the calculator to Programming mode by making the selection to binary, enter the binary number 10111110 and view the decimal equivalent.

    Did you get the correct answer? If not, retry the method and check to see what you did wrong.

  4. Perform a manual conversion of the decimal number 55 to an 8-bit binary number.
    What method (displayed above) will you use? Use a calculator to check your work.

  5. Perform a manual conversion of the octal number 461 to an 8-bit binary number.
    What method (displayed above) will you use? Use a calculator to check your work.

  6. Perform a manual conversion of the 8-bit binary number 11110001 to a hexadecimal number.
    What method (displayed above) will you use? Use a calculator to check your work.

  7. Perform a manual conversion of the hexadecimal number ABC to an 8-bit binary number.
    What method (displayed above) will you use? Use a calculator to check your work.

  8. Perform a manual conversion of the binary number 10101111 to an octal number.
    What method (displayed above) will you use? Use a calculator to check your work.

  9. Perform a manual conversion of the same binary number 10101111 to a hexadecimal number.
    What method (displayed above) will you use? Use a calculator to check your work.

  10. Perform a manual conversion of the octal number 5636 to a hexadecimal number.
    What method (displayed above) will you use? Use a calculator to check your work.

  11. Perform a manual conversion of the hexadecimal number D68 to an octal number.
    What method (displayed above) will you use? Use a calculator to check your work.

  12. When you have performed all of the numbering conversions above, then you can proceed to the next INVESTIGATION.

INVESTIGATION 2: FILE PERMISSIONS

In this section, you will get experience using the chmod and umask commands to set permissions for directories and files.

Perform the Following Steps:

  1. Login your matrix account and issue a command to confirm you are located in your home directory.

LINUX PRACTICE QUESTIONS

The purpose of this section is to obtain extra practice to help with your assignment #1, quizzes, your midterm, and your final ezam.

Here is a link to the MS Word Document of ALL of the questions displayed below but with extra room to answer on the document to simulate a quiz:

https://ict.senecacollege.ca/~murray.saul/uli101/uli101_week4_practice.docx

Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).

Number-conversion-chart.png

Review Questions:

  1. List the number of digits for the following numbering systems:
    • Decimal
    • Binary
    • Octal
    • Hexadecimal

  2. Write a simple chart to show which values are represented for letter A - F for a hexadecimal number.
  3. How many binary digits does 1 octal digit represent?
  4. How many binary digits does 1 hexadecimal digit represent?
  5. Use manual numbering conversion to complete the table displayed to the right.


  1. Write the chmod command (using the symbolic method) to set “pass-through” permissions
    (eg. r w x - - x - - x) for your home directory using an absolute pathname.
    Write a Linux command to verify that permissions where set.
    Perform a binary to octal numbering conversion for the permissions: r w x - - x - - x

  2. Write single Linux command to set “pass-through” permissions for your home directory,
    but use the absolute method (i.e. octal numbers) and use a relative-to-home pathname.

  3. Write a single Linux command to add read permissions for same group members for the “tests” directory.
    Use a relative pathname. Use the symbolic method.

  4. x
  5. x
  6. x
  7. x