Question

In: Computer Science

create "bitty.c" that uses bitwise operators and return statements to implement the functions in "bitty.h". "bitty.c"...

create "bitty.c" that uses bitwise operators and return statements to implement the functions in "bitty.h".

"bitty.c" that can only implement "bitty.h"

_____________________________________________________________

bitty.h:

// Returns 1 if v is an even number, otherwise returns 0

unsigned char isEven(unsigned char v);

// Returns 1 if v is zero, otherwise returns 0

unsigned char isZero(unsigned char v);

// Returns 1 if a and b have the same value, otherwise 0

unsigned char equals(unsigned int a, unsigned int b);

// Returns a if a is greater than or equal to b, otherwise returns b

// You may use if but may not use comparison operators or else

// e.g. if(a) is allowed but if(a<b) and if(b==0) are not allowed

unsigned char geq(unsigned char a, unsigned char b);

// Returns the number of bits that are 1

unsigned char tally(unsigned int n);

Solutions

Expert Solution

If you understand the concept do give it a like:)

unsigned char isEven(unsigned char v){

if(v & 1)

return 0;

return 1;

}

Explanation:  If v is even then rightmost bit must be equal to zero, so after anding it with 1 it will always give answer as 0

unsigned char isZero(unsigned char v){

if(v | 1)

return 0;

return 1;

}

Explanation:  If v is zero then ORing it with 1 will always give answer as 0.

unsigned char equals(unsigned int a, unsigned int b){

if(a ^ b)

return 0;

return 1;

}

Explanation:  XOR of 2 same numbers is always zero as XOR of same digit is 0.

unsigned char geq(unsigned char a, unsigned char b){

if(!(a ^ b))

return a;  

if(!(b ^ 0) || (a / b)))){

return a;

}

return b;

}

Explanation:  Here a ^ b refers to a == b , !(b ^ 0) refers to b == 0 and a/b will give us quotient as 0 if a<b or any other number if a>=b .

So, a ^ b gives us zero that means they are equal so we will return a

else we will check that if b == 0 OR a / b> 0 then we will return a other wise we will return b.

unsigned char tally(unsigned int n){

int count = 0;

while(n){

if(n & 1)

count++;

n>>=1;  

}

return count;

}

Explanation: Here we are checking each bit by Anding it with 1 if we encounter 1 then we will update our counter.

I hope I am able to solve your problem, if yes then do give it a thumps up :)


Related Solutions

Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
Variable Register a $9 b $19 c $2 C Operators + add - subtract & bitwise...
Variable Register a $9 b $19 c $2 C Operators + add - subtract & bitwise and ! bitwise or ~ bitwise not Assume the variables, a, b and c are stored in the registers given above. Give a single MIPS assembly instruction from the MIPS Core Instruction Set that performs the equivalent operation for each of the following C statements. a = ~ ( b | c ); // MIPS equivalent: -------- c = b - 2; // MIPS...
Java the goal is to create a list class that uses an array to implement the...
Java the goal is to create a list class that uses an array to implement the interface below. I'm having trouble figuring out the remove(T element) and set(int index, T element). I haven't added any custom methods other than a simple expand method that doubles the size by 2. I would prefer it if you did not use any other custom methods. Please use Java Generics, Thank you. import java.util.*; /** * Interface for an Iterable, Indexed, Unsorted List ADT....
Not all operators are commutative, namely, a*b = b*a. Consider functions under the operators addition, subtraction,...
Not all operators are commutative, namely, a*b = b*a. Consider functions under the operators addition, subtraction, multiplication, division, and composition. Pick two of the functions and use a grapher (like Desmos) to graph the functions under the operators. If the functions are commutative under the operator, the graphs should be identical (overlap everywhere) when you change the order in which the functions are entered with the operator. Determine which operators seem to the commutative and which operators seem not to...
IN C ONLY (follow instruction please) Implement the following functions: Queue * initQueue( ) // Return...
IN C ONLY (follow instruction please) Implement the following functions: Queue * initQueue( ) // Return empty queue // enqueue and dequeue each return an int error code int enqueue(int, Queue *) int dequeue(Queue *, int *) int getQsize(Queue *) // returns # of items in queue void freeQueue(Queue *) // Free *all* space Implement the above using a circular-linked list. Again, all operations except freeQueue should take O(1) time. You will need to document each of your functions to...
Create initializer/constructor functions for book. Declare and implement functions like insert book(insert at End, InsertAtFirst), retrieve...
Create initializer/constructor functions for book. Declare and implement functions like insert book(insert at End, InsertAtFirst), retrieve book, update book information book using ISBN number in Library. Declare another function named Search to find a specific book from catalogue. #include<iostream.h> #include<conio.h> struct book{ char name[50]; int ISBN; char authorname[50]; char publishername[50]; int issuedate; int issuemonth; int issueyear; int retdate; int retmonth; int retyear; struct book *next=NULL; } void insert(struct book *head){ if *head.next==NULL{ struct book node; cout<<"enter the name of book";...
USE ONLY THE BELOW FUNCTIONS AND IMPLEMENT THE MISSING PART implement the following missing functions from...
USE ONLY THE BELOW FUNCTIONS AND IMPLEMENT THE MISSING PART implement the following missing functions from the implementation: * reset * intersection * difference Set Set::intersection(Set& s){ Set r; // find intersection return r; } Set Set::difference(Set& s){ Set r; // find difference return r; } void Set::reset(int c){ // increase the capacity and clear the data } driver program int a1[] = {10,5,7,3,9}; Set s1(5); s1.insert(a1,5); s1.print("s1"); int a2[] = {2,9,6}; Set s2(3); s2.insert(a2,3); s2.print("s2"); Set s3 = s1.unionset(s2);...
Write a C++ program that uses all the relational operators.
Write a C++ program that uses all the relational operators.
C++ Program: Create a 3x3 matrix of int values. Implement five functions, each expecting the matrix...
C++ Program: Create a 3x3 matrix of int values. Implement five functions, each expecting the matrix as parameter input. The first function must use a nested loop to prompt a user to enter each value. Show the indices when prompting for input and populate the matrix with these values. The second function outputs the matrix and formats the output to align all columns and rows to accommodate values up to 1000. The third function finds and returns the minimum value...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT