0

I would like to modify a char by shifting it (and thus changing its value) and the return the result instead of that previous char:

#include <stdio.h>
#include <stdlib.h>

int main(){
    char * str = calloc(4, sizeof(char));
    printf("what bit to set [0-31]\n");
    int a;  
    scanf(" %i", &a);
    int byte = a/8;
    int offset = a%8;
    
    str[byte] = (str[byte] >> offset) & (char)255; //how to return the modified char? 
    
    for(int i =0; i<4 ; i++)
    {
        for (int j =0; j<8 ; j++)
            printf("%i", (str[i] >> (7-j)) & 1); //print the results
        printf("\n");
    }
}

output:

what bit to set [0-31]
20
00000000
00000000
00000000 // I want 00001000
00000000
milanHrabos
  • 2,010
  • 3
  • 11
  • 45

0 Answers0