Task

Implement the function get_prev_status

  • Write a function that takes a pointer to a block_header struct
  • The block_header struct contains a single size_t field called size_status, which encodes the block size and status bits
  • The second lowest bit (bit 1) of the size_status field indicates whether the previous block is allocated (1) or free (0)
  • Your function should return 1 if the previous block is allocated or 0 if it is free

Function Signature

// Given a pointer to a block_header, return 1 if the previous block is allocated, 0 if it is free.
int get_prev_status(struct block_header* header);

Example

Encoded size_status valuePrev Allocated? (Output)
0x00000000000000400
0x00000000000000421
0x00000000000000410
0x00000000000000431
0x00000000000001000
0x00000000000001021
0x00000000000001010
0x00000000000001031
0x00000000000002000
0x00000000000002021
0x00000000000002010
0x00000000000002031
0x00000000000010000
0x00000000000010021
0x00000000000010010
0x00000000000010031

Code

#include <stddef.h>
 
struct block_header {
    size_t size_status;
};
 
size_t get_prev_status(struct block_header* block) {
    size_t dif = block->size_status >> 1;
    if (dif % 2 == 1) return 1;
    return 0;
}