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 ...

Nibble Swapping in C (Hexadecimal Input) in C

Nibble Swapping in C

๐Ÿ”ถ Nibble Swapping in C (Hexadecimal Input)

#include<stdio.h>
int main( )
{
    unsigned int num;
    printf("Enter the number(in hex):");
    scanf("%x",&num);
    num = num & 0xFF;
    unsigned int swapped = ((num & 0x0F) << 4) | ((num & 0xF0) >> 4);
    printf("After swapping number is: %02X\\n", swapped);
    return 0;
}
  

๐Ÿ“˜ Explanation:

This C program swaps the upper and lower 4-bit nibbles of an 8-bit hexadecimal number.

๐Ÿ”น `scanf("%x", &num);` reads a hexadecimal value.
๐Ÿ”น `num & 0xFF` ensures the value is within 8 bits.
๐Ÿ”น `(num & 0x0F) << 4` moves the lower nibble to the upper nibble position.
๐Ÿ”น `(num & 0xF0) >> 4` moves the upper nibble to the lower nibble position.
๐Ÿ”น The final result is obtained by combining both using bitwise OR (`|`).
๐Ÿ”น `%02X` prints the swapped result in uppercase hexadecimal format with leading zero if needed.

๐Ÿ” Sample Output:

Enter the number(in hex):3C
After swapping number is: C3

Enter the number(in hex):F0
After swapping number is: 0F
    

๐Ÿท️ Keywords:

bitwise operations in C, swap nibbles C, C program hex input, 8-bit nibble swap, binary logic in C, nibble masking, hex manipulation

Comments

Popular Posts

๐ŸŒ™