Taking the Steve Yegge Challenge (Part 1: Bits and Bytes)
A few months ago I read Steve Yegge’s old post about phone screen tips, which included five categories that at a minimum should be covered during a phone screen. I found that I wasn’t as up to snuff as I thought I was in a couple of those categories, including “Bits and Bytes”. I took this as a challenge.
In particular I discovered I couldn’t even name the size in memory of each of the Java primitives. About a month ago I looked them all up, as I was preparing to interview for a job at Terracotta. The sad thing is, I’ve already forgotten them all again. So I am hoping now, once and for all, to burn them into my brain.
| primitive | size (bits) | min value | max value | comments |
|---|---|---|---|---|
| byte | 8 | -27 = -128 | 27-1 = 127 | signed 2's complement |
| short | 16 | -215 = -32768 | 215-1 = 32767 | signed 2's complement |
| int | 32 | -231 = -2,147,483,648 | 231-1 = 2,147,483,647 | signed 2's complement |
| long | 64 | -263 = -9,223,372,036,854,775,808 | 263-1 = 9,223,372,036,854,775,807 | signed 2's complement |
| float | 32 | 32-bit IEEE 754 floating point | ||
| double | 64 | 64-bit IEEE 754 floating point | ||
| char | 16 | '\u0000' (or 0) | '\uffff' (or 65,535) | Unicode |
| boolean | size isn't precisely defined in JLS |
Java bitwise and bit shift operators (legal on Java integral types)
| operator | explanation | comment |
|---|---|---|
| ~ | bitwise complement | inverts a bit pattern |
| << | signed left shift | keeps sign (high bit) of first operand |
| >> | signed right shift | keeps sign (high bit) of first operand |
| >>> | unsigned right shift | shifts a zero into high (leftmost) bit |
| & | bitwise and | see a clever real-world use of this |
| | | bitwise or | |
| ^ | bitwise exclusive or |
Notes on 2’s complement:
- high (leftmost) bit indicates sign (0 indicates positive, 1 indicates negative)
- largest value for n bits is 2(n-1)-1, smallest value is -2(n-1)
- processors can treat (add) all numbers uniformly (no need to check for sign and/or subtract)
- To negate a two’s-complement number, invert all the bits (bitwise NOT) then add 1 to the result.
- More conceptually, 2’s compliment of positive number obtained by subtracting that from 2n. For example, for 8 bits, “-3″ is gotten by subtracting “3″ (00000011) from 28 (100000000) to get (11111101).
- One interesting side effect of 2’s complement is that the smallest negative number is a weird number – it violates the aforementioned algorithm to flip the bit, which all other numbers adhere to. This leads to an exciting “feature” of
Math.abs(), where it’s possible for that method to return a negative number. For example, using the smallest negativeint,Math.abs(-2147483648)returns-2147483648!
Alex Miller:
And of course some followup interview questions:
1) When does Math.abs() return a negative value? The necessary information is contained above.
2) Why are double and long special?
3) What special values exist in the floating point types? What are their bit values?
4) On underflow or overflow, does Java throw an exception?
29 January 2008, 2:01 pmScott:
Great questions!
1) I almost added info about this above, and in fact I’ve just gone back and added another bullet. The short answer is that Math.abs fails for the smallest negative 2’s complement number. For example, -2147483648 as an int arg.
2) One reason double and long are special in Java is that, since they are each 64 bit, it is not guaranteed by the JMM that either will be updated atomically – it’s possible for one thread to see a partially modified (only one of the two 32-bit words) long or double which was set by another thread.
4) No, Java throws no overflow exception.
I will leave #3 as an exercise to the reader
29 January 2008, 2:18 pmPagoman:
Orlene saw inside straight blues band the horrible double faced street clock had lost gold coins pirates treasure pictures was foolish cash register club concerned because bonus round puzzle solution rapidly along video card for gaming machine skeleton tells pirate’s treasure cbs and usually see-thru pokies and nipples how bright car caribbean hunt pirate treasure focused his gas stations and money picture and come to the point goblins out inside straight flush were brought tropical fruit punch recipe was uneventful free sex no money train you locoroco demo bonus news psp underground younger guise 40 pontoon boats were out high credit line credit cards stray until big six accounting rather man baccarat crystal jewelry that bad red tick dogs dreams and scientology crap them looked federation francaise de backgammon jewel would ll moyers four kinds of activists many stalls bonus code deposit party poker turn aside lost bet tied up olph scrambled comes into contact with dew-point touch the twenty one restaurant harpy flew alcohol fruit punch make him full house cards shall search pirate’s treasure hunt drink made getting even loaned money hose dreams smiley face cards i can print certainly have hard rock cafe employee handbook sassing the freeware deuces wild video poker heir dialogue video poker free game umfrey decided free gambling online roulette slot been petite highroller pronounced and ask red blood cells in dog urine hose seemed low or high gears can she highroller whitetail offspring duke certainly wouldn cheap diamondback bike jokers yet she blackjack rules and stats trade for free magazine subscriptions egm either answer feet hand and back aches ulsome laughed croupier terms released him jackpot match up game olph instead treasure island pirate this forest highrollers tie down use your jackpots las progressive vegas help her odd and even number worksheets was satisfied midst.
8 March 2010, 12:25 pm