DOC PREVIEW
Penn CIT 591 - Integer Types

This preview shows page 1-2-3-4-5 out of 16 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Integer TypesBits and bytesNumber systemsEverything is a number?CountingComputers use binary numbersUsing octal and hex numbersWriting octal and hex integersInteger typesFloating-point literalsFloating point typesNumber “width”Widening and narrowingCastsExample castsThe EndInteger TypesBits and bytes•A bit is a single two-valued quantity: yes or no, true or false, on or off, high or low, good or bad•One bit can distinguish between two cases: T, F•Two bits can distinguish between four cases: TT, TF, FT, FF•Three bits can distinguish between eight cases: TTT, TTF, TFT, TFF, FTT, FTF, FFT, FFF•In general, n bits can distinguish between 2n cases•A byte is 8 bits, therefore 28 = 256 casesNumber systems•The binary (base 2) number system uses two “binary digits, ” (abbreviation: bits) -- 0 and 1•The octal (base 8) number system uses eight digits: 0, 1, 2, 3, 4, 5, 6, 7•The decimal (base 10) number system uses ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9•The hexadecimal, or “hex” (base 16) number system uses sixteen digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, FEverything is a number?•Everything in the computer is stored as a pattern of bits–Binary distinctions are easy for hardware to work with•Numbers are stored as a pattern of bits–Computers use the binary number system•Characters are stored as a pattern of bits–One byte (8 bits) can represent one of 256 characters•So, is everything in the computer stored as a number?–No it isn’t, it’s stored as a bit pattern–There are many ways to interpret a bit patternCounting•To count up in any number system,–add 1 to the rightmost digit–if the result is higher than the largest digit,•set that digit to zero and carry to the next place•repeat addition of 1 and carrying as many times as necessary•Example: In hex, 4A6FF + 1 = 4A700Computers use binary numbers•People like to use decimal numbers•Computers use binary numbers–Java translates decimal numbers into binary–The computer does all its arithmetic in binary–Java translates binary results back into decimal•You occasionally have to use numbers in other number systemsUsing octal and hex numbers•Computers use binary, but the numbers are too long and confusing for people--it’s easy to lose your place•Octal or hex is better for people•Translation between binary and octal or hex is easy–One octal digit equals three binary digits 101101011100101000001011 5 5 3 4 5 0 1 3–One hexadecimal digit equals four binary digits 101101011100101000001011 B 5 C A 0 BWriting octal and hex integers•Integers are usually written in decimal notation: 7, 532, -28•To write a number in octal, just start with a zero: 02, 0657, -077–but don’t use the digits 8 or 9 !•To write a number in hexadecimal, start with 0x or 0X: 0xA, 0X43AB5, -0xFFFF–the “digits” A through F can be upper or lower caseInteger types•There are four integer types–byte – occupies one byte (surprise!)•can hold numbers in range –128 to 127–short – occupies two bytes•can hold numbers in range –32768 to 32767–int – occupies four bytes•can hold numbers up to + or – 2 billion–long – occupies eight bytes•can hold numbers up to about 19 digits•literals are written with a L suffix: 123456789LFloating-point literals•Floating-point literals are written with a decimal point: 8.5 -7.923 5.000•Floating-point numbers may also be written in “scientific notation”– times a power of 10•We use E to represent “times 10 to the”•Example: 4.32E5 means 4.32 x 105•float literals are written with a F suffix–Examples: 8.5F -7.923F 5.000F 4.32E5F–If you don’t have the F suffix, type double is assumedFloating point types•There are two floating-point types–float – occupies four bytes•Can hold numbers in the range 3.4E38 to 1.4E-45•Accuracy is about nine digits–double – occupies eight bytes•Can hold numbers in the range 1.7E308 to 4.9E-324•Accuracy is seventeen or eighteen digitsNumber “width”•Numeric types are considered wider or narrower than other numeric types–This is based partly on number of bytes occupied–Also based on how large a number it can hold•Java doesn’t mind if you assign a narrow value to a wide variable: int n = 3;•Java is not happy if you assign a wide value to a narrow variable: byte b = 7139946;Widening and narrowing•You can always assign a narrower value to a wider variable–This is called widening•You can do something special to assign a wide variable to a narrower variable–This is called narrowingdoublefloatlongintshortbyteCasts•You can convert (cast) one numeric type to another•When you widen, no explicit cast is necessary–But it doesn’t hurt•When you narrow, an explicit cast is required–This requirement is made to help avoid errors•Casting tells Java that the value in the wider type will fit in the narrower type•Java checks to make sure that the cast works, and gives you an error if it didn’tExample casts short s = 0;int i = 0;double d = 0.0; d = i; // legald = s; // legali = s; //legal i = d; // illegals = d; // illegals = i; // illegal i = (int) d; // legals = (short) d; // legals = (short) i; // legal d = 3.7E20;i = 50000; // The following give// runtime errors:s = (short) i;i = (int) d;The


View Full Document

Penn CIT 591 - Integer Types

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download Integer Types
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Integer Types and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Integer Types 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?