-2

So I have the below code which shifts a 32 bit int 6 bits to the left (s->data) then appends the last 6 bits of the int operand to the int s->data. I would like to use this code to create a function which takes an unsigned char x and copies x into the first 3 bytes of the int s->data leaving the final byte as 0. So for example, if we had x = 255 then s->data , in binary form, would be 11111111 11111111 11111111 000000000. Does anyone know how this can be achieved using the below code (dataCommand). So If i can only shift left by 6 bits and append 6 bits to the end of s->data, how can I get something of the form above?.

I know how to get say 255 from using s->data (we do dataCommand(128+64+(255/64))) followed by dataCommand(128+64+(255%64)). This is assuming s->data is 0 to begin with. So this would give 00000000 00000000 00000000 11111111. However , I would like something of the form 11111111 11111111 11111111 00000000.

I am really lost as to how to do this, so any help would be greatly appreciated. Below is the dataCommand function. Thank you. As always, it can be assumed s->data is 0 to begin with.

void dataCommand(int operand, state *s) {
    printf("DATA BEFORE IS %x\n", s->data);
    // shifts bits of current data fields six positions to left
    s->data = s->data << 6;
    // (operand & 63) masks 6 bits off the operand
    // then we combine 6 bits of data with 6 bits of operand
    s->data = (s->data | (operand & 63));
    printf("DATA AFTER %x\n", s->data);

}
avii
  • 53
  • 5
  • What type is `s->data`? `I would like to use this code to create a function which takes an unsigned char x` Why do you want to use _this_ code? Write a different code, why use this specifically? `If i can only shift left by 6 bits and append 6 bits to the end of s->data,` Why do you want to duplicate a byte 3 times using _this specific function_? Using _specifically_ shifts of 6 bits? Just write a different function that uses different code and use shifts of multiples of `8`. Also, `int s->data` - do not use `int`s for shifting -cast to `unsigned int` before doing any operations. – KamilCuk Dec 04 '21 at 11:49

1 Answers1

0

Comments in MyFunction below explain how to do this.

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>


typedef struct state { int x, y, tx, ty; unsigned char tool; unsigned int start, data; bool end;} state;


void dataCommand(int operand, state *s) {
    printf("DATA BEFORE IS %x\n", s->data);
    // shifts bits of current data fields six positions to left
    s->data = s->data << 6;
    // (operand & 63) masks 6 bits off the operand
    // then we combine 6 bits of data with 6 bits of operand
    s->data = (s->data | (operand & 63));
    printf("DATA AFTER %x\n", s->data);

}

void MyFunction(unsigned char x, state *s)
{
    /*  Create the target pattern that contains 0 in byte 0 and x in bytes 1,
        2, and 3.
    */
    uint32_t p = 0x01010100u * x;

    /*  For each six bits in p, or fragment thereof, in order from high bits to
        low bits, shift those six bits down to the low six bits and give that
        to dataCommand to insert into s->data.
    */
    dataCommand(p >> 6*5, s);
    dataCommand(p >> 6*4, s);
    dataCommand(p >> 6*3, s);
    dataCommand(p >> 6*2, s);
    dataCommand(p >> 6*1, s);
    dataCommand(p >> 6*0, s);
}


int main(void)
{
    state s;
    MyFunction(0x45, &s);
    printf("data = 0x%08x.\n", s.data);
}
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • thank you so much, I will have a read through this now – avii Dec 04 '21 at 12:41
  • what is the type of p >> 6*n where n is any value from 0 to 5? – avii Dec 04 '21 at 12:50
  • actually, this does not work and i get the following error signed integer overflow: 16843008 * 255 cannot be represented in type 'int' sorry . – avii Dec 04 '21 at 13:39