Parsing hexadecimal numbers to binary and iterating over bits
Develop an implementation of the toChar method for Converter. Creative Exercises IP address. Write a program IP. That is, take the bits 8 at a time, convert each group to decimal, and separate each group with a dot. For example, the binary IP address should be converted to Web Exercises Excel column numbering. Write a function elias that takes as input an integer N and returns the Elias Gamma code as a string. The Elias Gamma code is a scheme to encode the positive integers. To generate the code for an integer N, write the integer N in binary, subtract 1 from the number of bits in the binary encoding, and prepend that many zeros.
For example, the code for the first 10 positive integers is given below. Write a function that takes an integer input, reverse its bits, and returns that integer. Swap elements i and j if i and j are bit reversal of each other. Such permutations arise in the Fast Fourier Transform.
What do the following two code fragments do given integers a and b? It works provided a and b are not the same variables in which case both variables are zeroed out. Find the unique integer. Describe an elegant and efficient algorithm to identify the integer that appears only once.
Bit-whacking version of Gray codes Use bit-whacking operations and iteration instead of recursion to generate a gray code. Name your program BitWhackingGrayCode. Free the prisoners I. A warden meets with 17 new prisoners when they arrive. The warden tells them that they may meet today and plan a strategy, but after the meeting, each prisoner will be in solitary confinement and will not be able to communicate with one another.
The prison has a switch room with 17 switches that can be on or off, although the initial configuration is not revealed. There is one special setting of the 17 switches that if it is ever achieved will enable the prisoners to go free. Each hour the warden escorts one prisoner to the switch room, where the prisoner can flip at most one switch from on to off or off to on.
The warden can choose prisoners in arbitrary order, so one prisoner may be chosen four times in a row, or not at all. Design a strategy for the 17 prisoners so that they are guaranteed to be set free after some finite amount of time.
Free the prisoners II. Same premise as above, except that the switch room has 2 switches initially both off , and a prisoner must flip exactly one of the two switches upon entering the switch room.
At any time, a prisoner may declare "all 17 of us have visited the control room. The warden can choose prisoners in arbitrary order, so one prisoner may be chosen four times in a row, but each prisoner will be chosen infinitely often assuming they are never freed. Count the number of 1 bits. Write function that takes an integer input and returns the number of 1's in its binary representation. See if you can figure out how it works. Explain why the following function that often appears in job interviews for programmers correctly counts the number of 1 bits in the binary representation of its input.
If the input has k 1s, how many times does the while loop iterate? Repeat the previous exercise, but pre-compute a table to speed up the computation. You can use the bit counting function from the previous exercise to initialize it. A table of size 2 32 is likely prohibitive. Java library functions for bit-whacking. Re-implement the following static methods that defined in the Integer class. One method that sleazy spammers use to auto-generate email addresses is by enumerating all possible email addresses at a give domain, e.
This annoying tactic is called a dictionary or Rumpelstiltskin attack and explains why you sometimes receive spam on a new email address to which you haven't given to anybody. Breaking a gold chain. You have a gold chain with 14 links that you are going to use to pay an worker for 15 days at a fee of 1 gold link per day. It's possible to split the chain into 15 pieces by cutting 14 times.
Your goal is to pay the worker while only breaking the chain 3 times. The worker must receive exactly the right fraction of total payment after each day of work.
Write a Java program HammingEncoder. Write a Java program HammingDecoder. Modify your solutions to the previous two exercises so that the input bits are packed 8 to the byte. Prove that a k-digit decimal number can be represented in binary with no more than 4k bits. Sum of powers of 2. Compute the sum of powers of 2.
What value do you end up with on two's complement machine? CDDB and freedb are databases that allow you to look up CD information on the Web and display the artist, title, and song name.
Each CD has a nearly unique disc ID number which is used to query the database. Write a static method sumDigits that takes an integer parameter and returns the sum of the decimal digits in the integer. Write a program CDDB. The bit 8 hex digit ID number is computed from the length of the tracks on the CD and the number of tracks as follows: Explain why the following code fragment does not leave ABCD in variable a. How would you fix it? In Java, byte is a signed 8-bit integer.
The right-shift promotes b0 to a negative integer. The celebration is the most important party you have ever hosted. You've got bottles of wine you were planning to open for the celebration, but you find out that one of them is poisoned.
The actual poison exhibits no symptoms until somewhere around the 23rd hour, then results in sudden death. You have thousands of prisoners at your disposal. What is the smallest number of prisoners you must have to drink from the bottles to find the poisoned bottle? Describe how to simulate bit unsigned integers in Java.
First, are you sure that you really need an unsigned type. Comparison operators are easy to simulate by checking the sign bit. Division and remainder are the trickiest: Describe how to simulate 8-bit unsigned integers in Java.
Same advice as previous question. One place where it's nice to have unsigned integers is for a lookup table, indexed by the byte. With signed integers the index can be negative. Also, if b is a byte , then b automatically casts b to an int. This could be undesirable since b is signed. In many applications you need to remove the signed extended bits via b. Adding two short integers. Explain why the following code fragment fails.
Java automatically promotes the results of most integer operations to be of type int. Yes, this is rather quirky. Explain why b give weird results when b is of type byte.
In Java, byte is an 8-bit signed integer. Before the right shift, b is converted to an integer. What does the following code fragment from program Overflow.
Suppose a is a variable of type int. What does the following code fragment print out? What does the following code do to the integers stored in two different variables a and b? Any problems with overflow? What is the value of cnt after the following loop? Explain why the following Java code fragment correctly determines whether the integer n is a power of 2.
Use the definition of the positional notation to define the base -2 number system. There are two digits 0 and 1. Count from -7 to 7 in this system. The format consists of four integers, representing the red, green, and blue intensities from 0 not present to fully used , and also the alpha transparency value from 0 transparent to opaque.
The four 8-bit integers are compacted into a single bit integer. Write a code fragment to extract the four components from the RGBA integer, and to go the other way. One of the following computes min a, b , the other computes max a, b without branching. Explain how it works. Suppose you have an array consisting of 2 32 - 1 integers of type int such that no two integer appears more than once.
Obtain the char that corresponds to each value in a hexadecimal string. Convert a hexadecimal string to an int. Convert a hexadecimal string to a float. Convert a byte array to a hexadecimal string. This example outputs the hexadecimal value of each character in a string.
First it parses the string to an array of characters. Then it calls ToInt32 Char on each character to obtain its numeric value. Finally, it formats the number as its hexadecimal representation in a string.
This example parses a string of hexadecimal values and outputs the character corresponding to each hexadecimal value. First it calls the Split Char method to obtain each hexadecimal value as an individual string in an array. Then it calls ToInt32 String, Int32 to convert the hexadecimal value to a decimal value represented as an int.
It shows two different ways to obtain the character corresponding to that character code. The first technique uses ConvertFromUtf32 Int32 , which returns the character corresponding to the integer argument as a string.
The second technique explicitly casts the int to a char.