Part 1

**Reference Books:**

**DATA STRUCTURES****Just as learning to design programs is important, for this reason is theunderstanding the the correct format and usage that data. Allprograms usage some type of data. To design programs i beg your pardon workcorrectly, a great understanding of how data is structured will berequired. **

**This module introduces you come the various creates of data usedby programs. Us shall investigate just how the data is stored,accessed and also its usual usage within programs. **

**A computer system stores information in Binary format. Binary is anumber mechanism which provides BITS to keep data. **

**BITS****A little is the smallest aspect of details used by a computer.A little holds among TWO feasible values, **

You are watching: How many bytes does a char take up

You are watching: How many bytes does a char take up

Value | Meaning |

0 | OFF |

1 | ON |

A little bit which is turn off is also considered to be FALSE or no SET;a little bit which is top top is also considered to be TRUE or SET.

Because a solitary bit can only save two values, bits arecombined with each other into huge units in bespeak to host a greaterrange of values.

**NIBBLE****A nibble is a group of 4 bits. This gives a best number of16 feasible different values. **

** 2 ** 4 = 16 (2 come the strength of the variety of bits)It is useful, when managing groups that bits, to determinewhich bit of the team has the least value, and which bit has actually themost or biggest value. **

**The Least far-ranging Bit and also The many Sigificant Bit****This is deemed to be little bit 0, and also is constantly drawn in ~ the extremeright. The Most far-ranging bit is constantly shown ~ above the extremeleft, and also is the bit with the best value. **

**The diagram below shows a NIBBLE, and each bits position anddecimal weight value (for much more information, consult the module onNumber Systems). **

** 3 2 1 0 little Number +---+---+---+---+ | | | | | +---+---+---+---+ 8 4 2 1 Decimal Weighting worth MSB LSBLets consider an instance of converting binary worths intodecimal. **

** little bit 3 2 1 0 value 1 0 1 1 bit 3 is set, therefore it has actually a decimal weight value of 8 little 2 is not set, therefore it has a decimal weight worth of 0 little 1 is set, so it has actually a decimal weight worth of 2 little bit 0 is set, so it has actually a decimal weight worth of 1 including up every the decimal weight worths for every bit= 11 therefore 1011 in binary is 11 in decimal!For much more examples, top the module ~ above Number Systems. **

**BYTES****Bytes are a group of 8 bits. This comprises 2 nibbles, asshown below. **

** 7 6 5 4 3 2 1 0 bit Number +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ 128 64 32 16 8 4 2 1 Decimal Weighting value MSB LSBBytes are regularly used to keep CHARACTERS.They can likewise be offered to store numeric values, **

** 0 to 255 -127 to +128**

**Binary Coded Decimal **

**Binary code decimal digits (0-9) are represented using 4 bits.The valid combinations of bits and their particular values room**

Binary value | Digit |

0000 | 0 |

0001 | 1 |

0010 | 2 |

0011 | 3 |

0100 | 4 |

0101 | 5 |

0110 | 6 |

0111 | 7 |

1000 | 8 |

1001 | 9 |

The binary combine 1010 to 1111 room invalid and are notused.

If the computer system stores one BCD digit every byte, that is callednormal BCD. The unused nibble may be either all 0"s or every 1"s.

**If 2 BCD digits space stored per byte, its dubbed PackedBCD**. This occurs in data transmission whereby numbers are beingtransmitted end a communications link. Pack BCD reduce theamount the time spent transmitting the numbers, together each data bytetransmitted outcomes in the sending of two BCD digits.

Consider the storing of the digits 56 in packed BCD format.

7 6 5 4 3 2 1 0 little bit Number +---+---+---+---+---+---+---+---+ | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | +---+---+---+---+---+---+---+---+ MSB LSBThe upper nibble hold the worth 5, whilst the reduced nibbleholds the worth 6.

**Status and Boolean Variables****BOOLEAN variables usage a single bit to hold their value, for this reason canonly assume one of two possible states. This is one of two people 0(considered to it is in FALSE), or 1 (considered to be TRUE). **

**The computer handles every boolean variable as a single bit. Ifthe little is TRUE, climate is has actually a worth of 1. If the bit is FALSE,then it has the value 0. **

**When a team of bits space grouped with each other to form a limitedrange of values, this is recognized as a STATUS** variable.

Consider the case in a program where we must keep monitor ofthe variety of minutes a phone line is busy for (within thelimited range of 0 come 60). This does not call for the use of afull integer, so some programming languages permit you to specifythe variety of bits provided to allocate come variables with limitedranges.

The advantage of this approach, is the the storage room ofstatus variables have the right to be an unified together right into a solitary 16 or 32bits, resulting in a conserving of space.

Consider wherein a computer system allocates 16 bits of storage perstatus variable. If we had three standing variables, the spaceconsumed would be 48 bits. BUT, if all the status variables couldbe combined and fitted right into a solitary 16 bits the storage, we couldsave 32 bits the memory. This is really important in real-timesystems wherein memory room is in ~ a premium.

Consider the adhering to diagram, i m sorry illustrates the packingof boolean and also status variables together into a solitary byte.

7 6 5 4 3 2 1 0 bit Number +---+---+---+---+---+---+---+---+ | 1 | 0 | 0 | 1 | 1 | 0 | 1 | 0 | +---+---+---+---+---+---+---+---+ | | | | | | | +--- regional call/TOLL contact (bit 0) | | +------- extension busy/free (bit 1) | +------------------- minute (bits 2-4) +----------------------- expansion diverted/TRUE/FALSE

**The American standard Code for information Interchange****ASCII is a computer code which supplies 128 various encodingcombinations that a group of 7 bits (27 = 128) to represent, **

**characters A to Z, both upper and also lower situation one-of-a-kind characters, number 0 to 9 special control codes offered for an equipment control**

Lets currently look in ~ the encoding method. The table listed below showsthe bit combinations forced for every character.

00 | 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 0A | 0B | 0C | 0D | 0E | 0F | |

00 | NUL | SOH | STX | ETX | EOT | ENQ | ACK | BEL | BS | TAB | LF | VT | FF | CR | SO | SI |

10 | DLE | DC1 | DC2 | DC3 | DC4 | NAK | SYN | ETB | CAN | EM | SUB | ESC | FS | GS | RS | US |

20 | ! | " | # | $ | % | & | " | ( | ) | * | + | , | - | . | / | |

30 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | : | ; | ? | |||

40 | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | |

50 | P | Q | R | S | T | U | V | W | X | Y | Z | < | > | ^ | _ | |

60 | ` | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o |

70 | p | q | r | s | t | u | v | w | x | y | z | ~ | DEL |

See more: How To Compare Fractions With The Same Numerator, Compare Fractions With The Same Numerator

A computer system usually stores details in eight bits. Theeighth little is unused in ASCII, hence is normally set to 0. Somesystems might use the eight bit to implement graphics or differentlanguage symbols, ie, Greek characters.

**Control codes are supplied in communications and printers. Theymay be created from an ASCII keyboard by holding down the CTRL**(control) vital and pressing another an essential (A to Z, to add {, , >, ^,Example password the message string "Hello." in ASCII usinghexadecimal digits.

H = 48 e = 65 together = 6C together = 6C o = 6F . = 2Ethus the wire is represented by the byte sequence

48 65 6C 6C 6F 2E

**CHARACTERS****Characters room non-numeric symbols offered to convey language andmeaning. In English, lock are an unified with other characters toform words. Examples of characters are; **

** a b c d e f g h ns j k l m n o ns q r s t u v w x y z A B C D E F G H ns J K together M N O ns Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # $ % ^ & * ( ) _ - = + | ` , . / ; " < > : " ? A computer system system generally stores characters using the ASCIIcode. Every character is stored making use of eight bits that information,giving a total number of 256 different characters (2**8 = 256). **

**In the high level language Pascal, characters are characterized andused as follows, **

** var plus_symbol : char; begin plus_symbol := "+"; Variables supplied in a Pascal routine are declared after thekeyword var**. The over example declares the change plus_symbolto it is in a character type, for this reason eight bits of storage storage areallocated to store its value (as however undetermined).

Inside the main body of the program, ~ the keyword **begin**,the statement shown assigns the symbol **+** to the charactervariable plus_symbol. This is indistinguishable to storing theASCII worth 2B hexadecimal in the eight bits of storage allocatedto the variable plus_symbol.

**TEXT STRINGS****Text strings space a sequence of personalities (ie, native or multi-character symbols). Each character is save one after the other,each occupying eight bits of memory storage. **

**The text string Hello would be save as complies with **

** +------+ | 48 | +------+ | 65 | +------+ | 6C | +------+ | 6C | +------+ | 6F | +------+ In Turbo Pascal, text strings room defined and used together follows,**

** var text_message : string<22>; begin text_message := "Welcome to text strings"; The over example declares the variable text_message to be astring form of as much as 22 characters long (but no more!). Eightbits of storage storage room allocated to save each character inthe cable (a full of 22 bytes), through the value in each byte asyet undetermined. **

**Inside the key body of the program, after the keyword begin,the statement displayed assigns the post Welcome come textstrings come the string change text_message. Thisstores the ASCII value of each character into each successivebyte of memory allocated to the variable text_message**.

**INTEGERS ****Numeric details cannot properly be stored using the ASCII format. Imagine save thenumber 123,769 making use of ASCII. This would consume 6 bytes, and also itwould be difficult to call if the number was hopeful or negative(though we could precede it through the character + or -). **

**A an ext efficient means of storing numeric details is come usea different encoding scheme. The encoding plan in most use isshown below, **

** Bits** 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 +---+--------------------------------------------+ | S | Numeric worth in Binary |+---+--------------------------------------------+ S = Sign bit Integers store totality numbers only! They perform not containfractional parts. Take into consideration the examples below,

**ValidInvalid **123.987 00.0 278903123.09 The sign bit (which is little 15) suggests whether the number ispositive or negative. A reasonable 1 shows negative, a reasonable 0indicates positive.

The number is convert to binary and stored in bits 0 to 14of the two bytes.

**Example** store the value +263 together an integer value. 1) The sign bit is 0. 2) The decimal value +263 is 100000111 in binary. Therefore the warehouse in memory of the creature +263 look at like, Bits 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 +---+---------------------------------------------+ | 0 | 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 | +---+---------------------------------------------+ once storing an unfavorable numbers, the number is stored making use of thetwo"s match format.

In Pascal, integers room defined and also used as follows,

var whole_number : integer; start whole_number := 1267; The instance declares the variable whole_number to be aninteger type, for this reason sixteen bits of storage storage space allocatedto keep its value (as yet undetermined).

Inside the main body that the program, after ~ the keyword **begin**,the statement displayed assigns the numeric value 1267 come the integervariable whole_number. This is indistinguishable to storing thebit mix 0000010011110011 in the sixteen bits the memoryallocated come the change whole_number.

Signed integers using 16 bits have a number range of,

-32768 to +32767 (+-2^15) come store bigger integer values would require more bits. Somesystems and also languages also support the use of unsigned integers,which are considered to be optimistic only.

**FLOATING allude NUMBERS****There room two problems with integers; they can not expressfractions, and also the range of the number is minimal to the numberof bits used. One efficient means of storing fractions is referred to as thefloating point method, i beg your pardon involves splitting the portion intotwo parts, an exponent and also a mantissa. **

**The exponent to represent a value elevated to the power of 2. **

**The mantissa to represent a spring value between 0 and also 1. **

**Consider the number **

** 12.50 The number is first converted into the style **

** 2n * 0.xxxxxx where n **represents the exponent and 0.xxxxx is themantissa.

The computer industry agreed ~ above a conventional for the storageof floating allude numbers. That is referred to as the IEEE 754 standard,and uses 32 bits of storage (for single precision), or 64 bits(for dual precision accuracy). The solitary precision formatlooks like,

Bits 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 +---+-----------------------+---------------------------------------------------------------------+ | S | Exponent value | Mantissa value | +---+-----------------------+---------------------------------------------------------------------+ S = Sign bit The sign little is 1 for a negative mantissa, and 0 because that apositive mantissa.

The exponent uses a bias of 127.

The mantissa is stored as a binary value utilizing an encodingtechnique.

**Working the end the FP little bit patterns****The number we have is **

**12.5 i m sorry expressed as fraction to the power of 2 is, **

** 12.5 / 2 = 6.25 6.25 / 2 = 3.125 3.125 / 2 = 1.5625 1.5625 / 2 = 0.78125 NOTE: Keep separating by 2 it spins a portion between 0 and 1results. The fraction is the mantissa value, the number ofdivisions is the exponent value. **

**thus our values now are, **

** 0.78125 * 24 The exponent bit pattern is stored making use of an excess of 127.This way that this value is added to the exponent as soon as storing(and subtracted once removing). **

**The exponent bit pattern to keep is, **

** 4 + 127 = 131 = "10000011" together the mantissa is a hopeful value, the sign little is 0. **

**The mantissa is a tiny more complex to work-related out. Eachbit to represent 2 come the power of a negative number. That lookslike, **

** 1st bit of mantissa = 0.5 second = 0.25 3rd = 0.125 fourth = 0.0625 5th = 0.03125 etc The mantissa number value we have actually is 0.78125, which in binaryis **

**11001000000000000000000(0.5 + 0.25 + 0.03125) How-ever, to do matters even more complicated, the mantissais normalized, by relocating the little bit patterns come the left (each shiftsubtracts one native the exponent value) till the very first 1 dropsoff. **

**The resulting pattern is then stored. **

**The mantissa currently becomes **

** 10010000000000000000000 and also the exponent is adjusted to end up being **

** 131 - 1 = 130 = "10000010" The last assembled style is, **

** Bits 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 +--+-----------------------+---------------------------------------------------------------------+ | 0| 1 0 0 0 0 0 1 0| 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | +--+-----------------------+---------------------------------------------------------------------+ S Exponent Mantissa now lets convert the adhering to storage format back into adecimal number. **

** Bits 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 +--+-----------------------+---------------------------------------------------------------------+ | 1| 1 0 0 0 0 0 1 1| 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | +--+-----------------------+---------------------------------------------------------------------+ S Exponent Mantissa This offers a negative number with an exponent worth of, **

** 131 - 127 = 4 and a mantissa worth of, **

** 1.0 + 0.5 + 0.0625 = 1.5625 (the 1.0 comes from the little which to be shifted off when themantissa to be normalized ), thus the number is, **

** -1.5625 * 24 = -25.0 The numeric variety for floating allude numbers utilizing theIEEE-754 method, using 32 bits, is **

** +- 0.838860808 * 2-128 come +- 0.838860808 * 2127 or, in decimal, **

** 2.4652 * 10-39 to 1.4272 * 1038 In Pascal, floating allude numbers are defined and used asfollows, **

**var fp_number : real; begin fp_number := 12.50; The instance declares the variable fp_number to be afloating- suggest type, thus thirty-two bits of memory storage areallocated to store its value (as however undetermined). **

**Inside the main body that the program, ~ the keyword begin**,the statement shown assigns the numeric worth 12.50 come the realvariable fp_number. This is equivalent to storing the bitcombinations 01000001010010000000000000000000 in the thirty-twobits of memory allocated come the change fp_number.

The benefits of save floating allude numbers in this wayare,

multiplication is perform by adding exponents and also mantissa"s department is carry out by individually exponents and also mantissa"s the is easy to compare 2 numbers to view which is the greater or lesser huge number ranges are stored using relatively couple of bitsThe defect of the storage format are,

errors are created by moving the mantissa bits conversion backwards and also forwards takes time