Structure with bit-fields (basic idea) Dry Run in C

Structure with bit-fields (basic idea) is an interactive C dry run visualizer from the Structures programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Structure with bit-fields (basic idea) Program Code

#include <stdio.h>

// Bit-field structure - members occupy specified number of bits
struct Status {
    unsigned int powerOn  : 1;  // 1 bit (0 or 1)
    unsigned int connected: 1;  // 1 bit
    unsigned int error    : 1;  // 1 bit
    unsigned int reserved : 5;  // 5 bits (unused)
    // Uses 8 data bits; sizeof may be larger due to compiler storage/alignment
};

struct RGBColor {
    unsigned int red   : 5;  // 5 bits (0-31)
    unsigned int green : 6;  // 6 bits (0-63)
    unsigned int blue  : 5;  // 5 bits (0-31)
    // Uses 16 data bits; sizeof may be larger due to compiler storage/alignment
};

int main() {
    struct Status device = {1, 0, 0, 0};
    
    printf("Device Status:\n");
    printf("Power On: %u\n", device.powerOn);
    printf("Connected: %u\n", device.connected);
    printf("Error: %u\n", device.error);
    
    // Modify bit-field
    device.connected = 1;
    device.error = 1;
    
    printf("\nAfter update:\n");
    printf("Connected: %u\n", device.connected);
    printf("Error: %u\n", device.error);
    
    struct RGBColor color = {31, 63, 31};  // Max values
    
    printf("\nColor: R=%u, G=%u, B=%u\n", color.red, color.green, color.blue);
    printf("Size of Status: %lu byte(s)\n", sizeof(struct Status));
    printf("Size of RGBColor: %lu byte(s)\n", sizeof(struct RGBColor));
    
    return 0;
}

View the complete Structures programs page.

Program Console Structure with bit-fields (basic idea) Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.