8 number-system

Post on 15-Apr-2017

256 Views

Category:

Internet

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Number System

05/03/232

05/03/233

05/03/234

05/03/235

05/03/236

05/03/237

05/03/238

05/03/239

05/03/2310

05/03/2311

05/03/2312

05/03/2313

05/03/2314

05/03/2315

05/03/2316

05/03/2317

05/03/2318

05/03/2319

05/03/2320

05/03/2321

05/03/2322

05/03/2323

05/03/2324

05/03/2325

05/03/2326

05/03/2327

05/03/2328

05/03/2329

05/03/2330

05/03/2331

05/03/2332

r’s and r-1’s COMPLEMENTS USED FOR

SIMPLIFY SUBTRACTION OPERATIONS

TWO TYPES Decimal Binary Octal Hexadecimal

r’ s complement 10’s 2’s 8’s 16’s r - 1’s complement 9’s 1’s 7’s 15’sHere r is base or radix of number system

r’s Complement LEAVE ALL LSD ZEROS UNCHANGED SUBSTRACT FIRST NONZERO LSD FROM r SUBSTRACT ALL OTHER HIGHER DIGITS FROM r-1

r-1’s Complement SUBSTRACT EVERY DIGIT FROM r-1

Note: r’s Complement=r-1’s Complement +1

34

RULES TO GET COMPLEMENTS9’S

SUBSTRACT EVERY DIGIT FROM 9 Example: 9’s complement of 134795 is 865204

10’s=(9’s complement +1)10’s complement can be Directly calculated by following steps:

LEAVE ALL LSD ZEROS UNCHANGED SUBSTRACT FIRST NONZERO LSD FROM 10 SUBSTRACT ALL OTHER HIGHER DIGITS FROM 9 Example:10’s complement of 134795 is 865205 Example: find the 9’s and 10’s complements of 314700.

2’S LEAVE ALL LS ZEROS AND FIRST NON ZERO DIGIT UNCHANGED REPLACE 1 BY 0 AND 0 BY 1 IN ALL HIGHER DIGITS

1’S 1 IS CHANGED TO 0 AND 0 IS CHANGED TO 1

Note: COMPLEMENT OF A COMPLEMENT RESTORES THE ORIGINAL VALUE OF THE NUMBER

10’s Complement Examples

35

-003

+1

996

997

-214

+1

785

786

Example #1

Example #2

Complement Digits

Add 1

Complement Digits

Add 1

Subtraction using r’s complement:

To find M-N in base r, we add M + r’s complement of N

1) If M > N then result is M – N + c (c is an end around carry and

Just neglect it. 2) If M < N then result is rn –(N-M) which is the

r’s complement of the result. (to get actual number take r’s

complement again and put – sign before number)

05/03/2336

Example

05/03/2337

Subtraction using r-1’s complement

The same rules apply to subtraction using the (r-1)’s complements.

The only difference is that when an end carry is generated, it is not discarded but added to the least significant digit of the result.

Also, if no end carry is generated, then the answer is negative and in the (r-1)’s complement form.

05/03/2338

05/03/2339

05/03/2340

05/03/2341

05/03/2342

05/03/2343

BINARY ARITHMETIC OPERATION

05/03/2344

BINARY ADDITION EXAMPLE

05/03/2345

BINARY ARITHMETIC OPERATION (Cont…)

05/03/2346

We have already discussed these two.

BINARY SUBTRACTION EXAMPLE

05/03/2347

05/03/2348

OCTAL ARITHMETIC OPERATION

05/03/2349

05/03/2350

05/03/2351

05/03/2352

05/03/2353

05/03/2354

05/03/2355

05/03/2356

05/03/2357

05/03/2358

Flowchart for steps in C program Execution

Practice problems 1. Given the binary number 11011011, what is its

decimal value if it is aa.unsigned binary numberb.Signed binary numberc.2's complement number

2. Convert +24 into a 2's complement number

05/03/2359

Ans: 219Ans: -37

Ans: -37

Ans: 011000 Note the leading zero!

3. Convert -24 into a 2's complement number

4. Convert -1 into a 2's complement number (assume a 4-bit result)

5. What is the minimum (maximum negative) number which can be

represented using a 6-bit 2's Complement representation? What is the maximum (positive) number?

05/03/2360

Ans: 1111

Ans: 101000

Ans: +31, -32

Practice problems (Cont…)

Practice problems (Cont…)

Convert, if possible, the following decimal numbers to 2's complement assuming an 8-bit binary representation for all.

i. 1 = ii. -1 =iii. 72 = iv. 127 =v. -127 = 10000001 vi. -105 = 10010111vii. 255 = viii. -255 = ix. -6 =

05/03/2361

00000001 1111111101001000 01111111

Not Possible 11111010

Not Possible

Practice problems (Cont…)

Convert the following 2's complement numbers to decimal.

i. 1111 = ii. 11110000 =iii. 001101 = iv. 11111111

= -1v. 0000000 = 0 vi. 01 = 1

05/03/2362

-1 -1613

05/03/2363

05/03/2364

As a general rule, it is a good idea to avoid the use operators that cause side effects inside of compound expressions. This includes all assignment operators, plus the increment and decrement operators.

05/03/2365

Note: As a general rule, it is a good idea to avoid the use of operators that alter the value of same variable more than one in the same expression, because this causes side effects. This includes all assignment operators, plus the increment and decrement operators.For all side effects result will be compiler dependent.

05/03/2366

#include<stdio.h>void main() {

int i=3;printf("%d",++i + ++i); o/p 9 for turbo c

10 by codepad} The results are undefined. You're modifying a variable more

than once in an expression (or sequence point to be more accurate).

Modifying a variable more than once between sequence points is undefined, so don't do it.

05/03/2367

05/03/2368

top related