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

Write a C expression (or a collection of sequential expressions) that will retur

ID: 3662769 • Letter: W

Question

Write a C expression (or a collection of sequential expressions) that will return the nth nibble of the int x. For this problem the index of the least significant nibble is 0, and the index of the most significant nibble is 15 (or F) (so 0 <= n <= F). So if x = 0x0123456789ABCDEF (a 64-bit integer), and n equals 3, the C expression(s) would return C, and if n equals C, the expression(s) would return 3. You cannot use, division, multiplication, or modulus, relative comparisons (<, >, <=, >=), loops, switches, function calls, macros, conditionals (if or ?:). You are allowed to use all bit level and logic operations, left and right shifts, addition and subtraction, equality and inequality tests, integer constants (<=255), INT_MIN and INT_MAX, and casting between data types.

Explanation / Answer

Information (often called data) inside a computer, as well as on the board used in this tutorial, is exchanged among the various components by means of metallic conductors called data lines. A group of data lines is called a data bus.

Each data line carries a unit of data called a bit. A bit can be on or off. On is usually considered to be 5 volts, and off is considered to be 0 volts, although modern systems often use lower on voltages to decrease power consumption.

Data can be represented on paper as a series of ones and zeros. A one means a bit is on, and a zero means it is off. A group of 8 bits is called a byte. A byte with a value of 0 would be represented as 00000000. Non-zero bytes can be any combination of 1s and 0s. 01100010 will be used as an example here. In the C language, a byte is called a character and is abbreviated char.

When data is represented as a series of ones and zeros, it is said to be a binary representation, or to have a base of 2 because it uses 2 digits. We humans use a base of 10, probably because we have 10 fingers.

The left-end bit of a number represented in binary is called the most significant bit, abbreviated msb, and the right-end bit is called the least significant bit, abbreviated lsb.

A common way of showing numbers in a C program is to use hexadecimal notation, or HEX. It uses a base of 16. Break a byte into two groups of 4 bits each: nnnn nnnn. Each group is called a nibble. A nibble with all low bits, 0000, is equal to 0. With all of its bits turned on, 1111, a nibble has a value of 15 (8 + 4 + 2 + 1). Thus, we are dealing with the 16 values from 0 through 15, and a base of 16.

Hexadecimal notation is simple. Just use digits for 0 through 9, and A through F for 10 through 15. The following table shows all of the combinations.

The right nibble of a byte is the least significant nibble. It's the 1's place because it's 160. Next is the 16's place because it's 161, then 162 = 256, and so on. To get the decimal value, take the value of the nibbles, multiply by the position weight values and add them up. Thus, the HEX value 9B = (9 * 16) + (11 * 1) = 155.

A nibble is a four-bit aggregation, or half an octet. There are two nibbles in a byte.
Given a byte, swap the two nibbles in it. For example 100 is be represented as 01100100 in a byte (or 8 bits). The two nibbles are (0110) and (0100). If we swap the two nibbles, we get 01000110 which is 70 in decimal.

To swap the nibbles, we can use bitwise &, bitwise ‘<<' and '>>’ operators. A byte can be represented using a unsigned char in C as size of char is 1 byte in a typical C compiler. Following is C program to swap the two nibbles in a byte.

#include<stdio.h>
#include<stdlib.h>
unsigned char swap(unsigned char c)
{
unsigned char t1, t2;
t1 = c & 0x0F;
t2 = c & 0xF0;
t1=t1 << 4;    
t2=t2 >> 4;    
return(t2|t1);
}

int main(void)
{
char c=0x34;
printf(" Test Value is %x ", c);
printf(" The Swapped value is %x ",swap(c));
system("pause");
return 0;
}

Binary Decimal Hexadecimal Binary Decimal Hexadecimal 0000 00 0 1000 08 8 0001 01 1 1001 09 9 0010 02 2 1010 10 A 0011 03 3 1011 11 B 0100 04 4 1100 12 C 0101 05 5 1101 13 D 0110 06 6 1110 14 E 0111 07 7 1111 15 F
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