User Tools

Site Tools


Sidebar

projects

pct0 (bonus; due 20190821)
cci0 (due 20190821)
wcp1 (due 20190821)
dtr0 (bonus; due 20190828)
pct1 (due 20190828)
wcp2 (due 20190828)
pct2 (due 20190904)
wcp3 (due 20190904)
sof0 (due 20190911)
pct3 (due 20190911)
wcp4 (due 20190911)
dow0 (due 20190918)
pct4 (due 20190918)
wcp5 (due 20190918)
mtf0 (due 20190925)
pct5 (due 20190925)
wcp6 (due 20190925)
mtf1 (due 20191002)
pct6 (due 20191002)
wcp7 (due 20191002)
bcf0 (due 20191010)
epf1 (due 20191010)
pct7 (due 20191009)
wcp8 (due 20191009)
cnv0 (due 20191023)
pct8 (bonus; due 20191023)
pct9 (due 20191023)
wcp9 (due 20191023)
cnv1 (due 20191030)
pctA (due 20191030)
wcpA (due 20191030)
fwf0 (due 20191106)
pctB (due 20191106)
wcpB (due 20191106)
cos0 (due 20191113)
pctC (due 20191113)
wcpC (due 20191113)
eoce (due 20191211 by 172959)
haas:fall2019:c4eng:projects:dtr0info

This is an old revision of the document!


dtr0 tips and tricks

Declaring variables

A variable is defined as: an element, feature, or factor that is liable to vary or change.

On the computer, a variable is memory location containing binary data. Often times, additional rules of interpretation will be applied to the binary data, pertaining to a certain format (integer data, floating point data, memory address data)

In C, variables need to be declared and ideally initialized before we use them. All variables need to be identified with some data format (otherwise known as a data type).

As talked about in class, and on the dtr0 project, there are a number of possible data types to choose from. For dtr0, we will be focusing on the integer types:

  • char
  • short int
  • int
  • long int
  • long long int

Each of these can store whole numbers, of differing ranges. The idea is to use the data type that “best fits” the scenario you are using it for.

ie, if you know you will ONLY need to use the values 0-100, while ALL of these integer types can accommodate such a request, there's one which can accommodate it while wasting the least amount of storage.

There is an additional qualifier we can associate with each of the integer types, and that is the indication of whether or not we wish to interact with negative numbers. The two qualifiers are:

  • signed - allow positive AND negative numbers
  • unsigned - no negative numbers at all

If you omit the signedness qualifier, it will likely default to a signed type.

To declare a short integer type named number with unsigned qualities:

unsigned short int number;

It is good programming practice to be as specific as possible. As such, when we declare variables, it is also a good idea to then initialize them to a known starting value (don't assume!). Here, we will initialize number to 0:

number  = 0;

We can combine both of these steps into one:

unsigned short int number  = 0;

Binary representation

The computer is a binary device. That means it stores and transacts all its data in base 2, where there are only 2 counting bits: 0 and 1.

Think of this as a light switch, which can be either turned off, or turned on. That's it.

The computer will group a number of these bits together when it stores, retrieves, or manipulates information. The common unit of transaction is the byte, which in modern days has been set to eight bits.

The computer can access data in units of bytes (typically in some power of two), and the amount of bytes (and therefore bits) we have within that collection of data denotes the number of different combinations which can be represented (one at a time).

For example, take a single bit. That “light switch”. How many possible combinations can exist with one light switch?

Two, right? It can either be ON, or it can be OFF. Nothing else.

We can use a mathematical expression to aid us in determining the number of possibilities, based on how many bits are present.

It is: 2^x=y

Where x is the number of bits, and y is the number of possibilities.

# bits number of possibilities
0 1
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256
9 512

If there are eight bits in a byte, a byte can store any one of 256 unique possibilities (but of course, it can only be ONE of those possibilities at any given time).

If we had two bytes, we would have sixteen total bits. How many total possibilities can sixteen bits represent?

Hexadecimal as a binary short hand

While the computer is a binary device, when interacting with the computer we will often use base 16 (hexadecimal), as a convenient short hand for interacting with the computer's information.

This is because, like 2, 16 is a power of two, so there are certain conveniences leveraged.

Mainly, as it takes four binary bits to represent sixteen unique possibilities, one hexadecimal counting symbol reflects four binary bits. Hexadecimal values will be much shorter than their binary counterparts, while both storing the same information, and not needing any complicated conversion process.

Take the following table:

base 2 base 8 base 10 base 16
(binary) (octal) (decimal) (hexadecimal)
0000 00 0 0x0
0001 01 1 0x1
0010 02 2 0x2
0011 03 3 0x3
0100 04 4 0x4
0101 05 5 0x5
0110 06 6 0x6
0111 07 7 0x7
1000 010 8 0x8
1001 011 9 0x9
1010 012 10 0xA
1011 013 11 0xB
1100 014 12 0xC
1101 015 13 0xD
1110 016 14 0xE
1111 017 15 0xF
haas/fall2019/c4eng/projects/dtr0info.1566575233.txt.gz · Last modified: 2019/08/23 15:47 by wedge