Skip to main content

Featured

Solve LeetCode 3 Longest Substring Without Repeating Characters in C++ using the sliding window

Longest Substring Without Repeating Characters in C++ LeetCode 3 · sliding window · last index array Table of Contents Problem Statement Approach Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Conclusion Problem Statement 3. Longest Substring Without Repeating Characters Given a string s , find the length of the longest substring that contains no repeating characters. A substring is a continuous part of the string, so "pwke" is a subsequence of "pwwkew" but not a substring. Approach We use a sliding window . The window is the part of the string between start and end , and we always keep it free of repeated characters. An array lastIndex of size 128 remembers the most recent position of every ASCII ...

Bit-Fields in C - Real World Example

Bit-Fields in C - Real World Example

๐Ÿ“Œ Bit-Fields in C – Real World Example

๐Ÿ”‹ 1. Embedded System: Status Flags using Bit-Fields

#include <stdio.h>

struct StatusFlags {
    unsigned int powerOn : 1;
    unsigned int error : 1;
    unsigned int batteryLow : 1;
    unsigned int reserved : 5;
};

int main() {
    struct StatusFlags device = {1, 0, 1, 0};

    printf("Power ON: %u\n", device.powerOn);
    printf("Error: %u\n", device.error);
    printf("Battery Low: %u\n", device.batteryLow);

    return 0;
}
  

๐Ÿง  Explanation:

  • struct StatusFlags uses only 8 bits (1 byte) instead of 4 bytes.
  • Each flag (like power, error) is a bit-field that takes just 1 bit.
  • Perfect for embedded systems where memory and performance are critical.

๐Ÿ–ฅ️ Sample Output:

Power ON: 1
Error: 0
Battery Low: 1
  

๐Ÿ”‘ Keywords:

bit field in C, struct with bit-fields, embedded systems flags, memory-efficient C code, real-world struct usage, flag register, C interview examples

Comments

Popular Posts

๐ŸŒ™