Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sparse Representation of Binary Numbers We can represent binary numbers as a lis

ID: 3750166 • Letter: S

Question

Sparse Representation of Binary Numbers We can represent binary numbers as a list of increasing integers where each element is a power of two: type nat-int list For example: 5 1 4] or 15-[1 2; 4; 8 or 17 16]. We can represent O with the empty ist. The sparse representation can be a more useful way of representing numbers than using a dense representation i.e. one using ones and zeroes), especially for human-readable arithmetic 1. Implement a functioninc nat -> nat which increments a given sparse binary number. 2. Implement a function dec : nat-> nat which decrements (i.e., subtracts one from) a given sparse binary number. If given 0 as input, you should raise the exception Domain 3. Implement a function add nat -> nat which adds two sparse binary numbers Implement a function sbinToInt nat -> int using a helper function toInt which translates a given sparse binary number to an integer tail-recursively using an accumulator

Explanation / Answer

given number is sparse or not

A number is said to be a meager number if in parallel portrayal of the number no at least two sequential bits are set. Compose a capacity to check if a given number is Sparse or not.

Model :

Info: x = 72

Yield: genuine

Clarification: Binary portrayal of 72 is 01001000.

There are no two back to back 1's in paired portrayal

Info: x = 12

Yield: false

Clarification: Binary portrayal of 12 is 1100.

Third and fourth bits (from end) are set.

public class Solution {

public static void main(String args[]) {

int x = 72;

System.out.println(convertToBinaryString(x));

int next_sparse = sparse_number(x);

System.out.println(next_sparse);

System.out.println(convertToBinaryString(next_sparse));

}

public static int sparse_number(int x) {

if(isSparseBinaryNumber(x)) {

int i = getFirstIndexSeqZeros(x);

if (i != -1) {

if (i - 1 == 0) {

return x + 1;

}

return incrementToNextSparseFromZeroIndex(x, i - 1);

} else {

return 1 << getLengthBits(x);

}

} else {

int i = getLastIndexSeqOnes(x);

return incrementToNextSparseFromIndex(x, i);

}

}

public static int incrementToNextSparseFromZeroIndex(int x, int index) {

int i = indx - 1;

int n_2 = x;

n_2 = setBit(n_2, indx, true);

while(i >= 0) {

n_2 = setBit(n_2, i, false);

i--;

}

return n_2;

}

public static int incrementToNextSparseFromIndex(int x, int indx) {

int i = 0;

int n_2 = x;

boolean prv = false, current = false;

while(i <= indx) {

n_2 = setBit(n_2, i, false);

i++;

}

n_2 = setBit(n_2, i, true);

prv = true;

i++;

current = getBit(n_2, i);

while (current && prv && n_2 > 0 || (n_2 < x && n_2 > 0)) {

n_2 = setBit(n_2, i - 1, false);

n_2 = setBit(n_2, i, true);

i++;

prv = current;

current = getBit(n_2, i);

}

if (n_2 == 0) {

return 1 << getLengthBits(x);

}

return n_2;

}

public static int getFirstIndexSeqZeros(int x) {

boolean curr, prv = false;

int i = 0;

while(x > 0) {

if ( (x & 1) == 0) {

curr = true;

} else {

curr = false;

}

if (curr && prv) {

return i;

}

prv = curr;   

  

i++;

x = x >> 1;

}

return -1;

}

public static int getLastIndexSeqOnes(int x) {

boolean curr, prv = false;

int i = 0;

int highest = -1;

while(x > 0) {

if ( (x & 1) == 1) {

curr = true;

} else {

curr = false;

}

if (curr && prv) {

highest = i;

}

prv = curr;   

  

i++;

x = x >> 1;

}

return highest;

}

public static boolean isSparseBinaryNumber(int x) {

boolean curr, prv = false;

while(x > 0) {

if ( (x & 1) == 1) {

curr = true;

} else {

curr = false;

}

if (curr && prv) {

return false;

}

prv = curr;

  

x = x >> 1;

}

return true;

}

public static int getLengthBits(int x) {

int count = 0;

while (x > 0) {

count++;

x = x >> 1;

}

return count;

}

public static String convertToBinaryString(int x) {

StringBuilder sb = new StringBuilder();

while(x > 0) {

if ( (x & 1) == 1) {

sb.append("1");

} else {

sb.append("0");

}

x = x >> 1;

}

return sb.reverse().toString();

}

public static boolean getBit(int x, int indx) {

return ((x & (1 << indx)) > 0);

}

public static int setBit(int x, int indx, boolean b) {

if (b) {

return x | (1 << indx);

} else {

return x & ~(1 << indx);

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote