Question: [C programming] I need help with writing a method that takes in a character, for example, A and a 2D array that consists of 'S'

[C programming]

I need help with writing a method that takes in a character, for example, A and a 2D array that consists of 'S' meaning swap or 'N' meaning don't swap. The 2D array will look something like this

SNSN

SNSN

SNSN

SNSN

This method iterates through the 2D array, in this case, table and if for example, table[3][0] == 'S' that means swap bits 7 and 6. The first row in the 2D array deals with bits in position 7 and 6, second-row deals with bits 5 and 4, third row deals 3 and 2, and fourth row deals with bits 1 and 0. The code below is what I have so far. Please provide code and explain how swapping bits work. I'm not knowledgeable on bit manipulation and how it works.

char encrypt(char ch, int table[4][4]) { char ret; int col, row;

for(col = 0; col < 4; col++) { for(row = 0; row < 4; row++) { if(table[row][col] == 'S') { if(row == 0) { //swap bits 7 and 6 in ch } else if(row == 1) { // swap bits 5 and 4 in ch } else if(row == 2) { // swap bits 3 and 2 in ch } else if(row == 3) { // swap bits 1 and 0 in ch } } } }

return ret; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!