1. Write a C expression (or a collection of sequential expressions) that will sw
ID: 3880488 • Letter: 1
Question
1. Write a C expression (or a collection of sequential expressions) that will swap the mth and nth byte of the 64-bit quantity (stored as a 64-bit integer, but is really 8 ascii characters). For this problem the index of the least significant byte is 0, and the index of the most significant byte is 7 (so 0 <= n,m <= 7). So if x = “hi calss” = 0x68692063616C7373 (a 64-bit quantity), and we want to swap the ‘a’ and the ‘l’ to get “hi class” we could have n equals 3 and m equals 2, the C expression(s) would return 0x686920636C617373 = “hi class”. 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. (20 points)
Explanation / Answer
#include <stdio.h>
int main(void) {
char C[10];
int m,n,i;
printf("Enter ASCII CHAR ");
for(i=1;i<9;i++) //for 8 Ascii char
{
scanf("%c",&C[i]);
}
printf("Enter m and n ");
scanf("%d%d",&m,&n);
// swapping using XOR logical operation
C[m]=C[m]^C[n];
C[n]=C[m]^C[n];
C[m]=C[m]^C[n];
for(i=1;i<9;i++)
{
printf("%c",C[i]);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.