Subj : Re: Really **wonderful** website for C interview questions!!!!! To : comp.programming From : rem642b Date : Wed Jul 06 2005 02:17 am > From: "Robert Bunn" > static_cast does that -- but that's not C. C doesn't even have > a "bool" type to cast to, does it? Yes, that's all correct. > So the idiom of (!!x) is pretty much the same as (x?1:0) if I read > and reason correctly. Very good! The latter is a more perspicuous than the former, in my opinion. It says clearly what value is returned in the two cases. Your example made me think: This would be a good kind of question on a written or online exam: The exam lists these expressions: !(x^y) (!!x)==(!!y) (x?1:0) (y=!!x) x&&y x==(!!y) y=(!!x) and then asks the student to collect them into equivalence classes per their semantics. > I give up. I'm completely guessing at the answer, but I can't think > of any reason I would ever use that construction. Simple: Normalization of data, a standard tool in data processing. You get words in upper or lower case or first letter capitalized, or even every first letter of word within compound word except first inner word capitalized, and you want to do a case-insensitive partitionning into equivalence classes. Standard method is (1) normalize them to all lower case, (2) put that form of each in hash table. It's a lot easier to pre-process each word by itself to convert to standard lower case and then do case-sensitive comparisons on the results, than to devise a case-insensitive comparison test to use directly on the original words as originally capitalized. Same idea applies here. Suppose you have a bunch of boolean values, i.e. they are integers but anything not zero should be treated as all the same. So you pre-process all the data to be just 0's and 1's, and then these uniformly-represented boolean values can be processed by bitmask/bitshift operations without strange bugs striking. You shouldn't give up when you had that perspicuous answer to the quiz question! Maybe you're really bright but are too shy to realize it? By the way, what would you call this code? z = (y?(x?1:0):(x?0:1)) I'm not asking what function it computes, rather what do you call the *style* of expressing the answer computed/organized with that particular structure. Hint, initials T.T. Don't look ahead, try to figure it out before I give away the answer below. - - - - - - - - - - Medium clue: Here's the above C code indented more revealingly: z = (y?(x?1:0) :(x?0:1)) Here's the above code translated to Lisp (where NIL instead of 0 is the single false value, and anything else is treated as "true"): (setq z (if y (if x T NIL) (if x NIL T ))) Do you see the picture now that it's indented better? Big Clue: This code does the same in a more obvious way. (setq table '(( T NIL) (NIL T ))) (setq x (random 2)) (setq y (random 2)) (setq z (nth x (nth y table))) P.S. many such algorithms are more perspicuous in Lisp than in just about any other language, because of the ability to define complex structures as literal constants. Anyway, do you recognize a T.T. now? Now for a real tough question on an exam (open book, open InterNet): Write a pair of functions (in whatever programming languge is being tested) to convert between ASCII and EBCDIC. Good student would search Google, find this: http://support.microsoft.com/kb/q216399/ and use that as guidance in writing the sofware. Hacker would write code to connect to that Web site and download the table and parse it to yield the local machine table to be used, in case there was a mistake in that table later corrected, the program would automatically import the fix next time it's run. Super hacker would write code to connect to Google and do the search and parse each match to see which looked like the best table, then download that table and parse it, in case the WebSite had moved but Google now had the new location indexed. Wise hacker would write code to do all that above, but also cache the ten best Google matches, cache the URL of the site actually selected, cache the downloaded raw table, cache the parsed table, to cover all possible breakdowns yet still get the latest version if the net is working and all sites are up. Maybe also parse several different tables and compare them to make sure the chosen one was correct. Smart-ass Hacker would find the Web site that mentions how HP and IBM and AT&T EBCDIC are different, hence the question is ambiguous.  .