Please answer the next 4 questions following the instructions of them -the way that the question ask for precisely- the first three parts in java – simple java. and the part 4 by c language.
—————————————————————–
Part 1. Using the bitwise operators (&, |, ^, ~, – (2’s complement/unary negation), <<, >>, and >>>) discussed in class, write a function in Java that swaps (returns) the byte order of an integer from one endian-ness to the other. A template for the Java function follows:
static int swap ( int i ) {
.
.
.
}
Part 2. Using only the the arithmetic operators (+, -, *, /, %, and – (2’s complement/unary negation)), implement a Java function that also swaps the byte order of an integer (ifs are OK too). Note that it is actually much more difficult to do this using only the arithmetic operators! Hint: First implement it correctly for positive i, and only then begin working on the version that also handles negative i. A template for this Java function follows:
static int swap2 ( int i ) {
.
.
.
}
Part 3. Using a timing methods such as the Java code below, time your swap and swap2 methods by repeatedly calling them many, many times. Report your results in the number of seconds required for one function call.
final int N = 1000000;
long start = System.nanoTime();
for (int i=0; i<N; i++) {
int x = swap( 0x01020304 );
}
long elapsed = System.nanoTime() – start;
System.out.println( “elapsed time = ” + (elapsed/1E9/N) );
Furthermore, since C and C++ have pointer variables, another approach is possible. But please use only the bitwise ones in part 1, and only the arithmetic ones in part 2. Do not use any built in functions that may exist, and certainly do not manipulate them as strings!
You are required to test your program using the following input: 0x01020304, 0x010203ff, 0xff020304, 0xfe020304, and 0x80aacc84. Email your code and output to me and our excellent TA.
Part 4. Write a C program with a function that changes the byte order of an integer by treating it as an array of characters. The function should be defined as the following:
void swap ( char* data ) {
…
}
It should be called like the following: int k = 0x0a0b0c0d;
swap( (char*) &k );
After swap, the byte order of k should become 0x0d0c0b0a.
For a custom paper on the above topic, place your order now!
What We Offer:
- On-time delivery guarantee
- PhD-level writers
- Automatic plagiarism check
- 100% money-back guarantee
- 100% Privacy and Confidentiality
- High Quality custom-written paper
You May Also Like This:
- MACRO intVal
- Network
- LED values
- Hoover, Inc., uses a job-order coding system. The company’s inventory balances on February 1, the start of its fiscal year
- Real Estate Data
- Nominal versus Real Returns
- Data structures and algorithms assessment
- NR351-10133 Week 4 Assignment: Professional Paper Worksheet
- Word sense disambiguation