C Codes

# Count Set Bits

C Program to count the set bits: Advertisements

C Codes

# Generate Permutations – Decrease and Conquer

Uses the definition of n! to generate the permutations. Idea: Remove each item from the given n items one at a time and append it to remaining (n-1)! permutations. Efficiency: O(n!) and as well we have expensive swaps Strategy used: Decrease and Conquer(decrease by 1)

General Discussions

# Multiples of 8

To check if the number is multiple of 8, the bitwise way is that: first right shift by 3 and then left shift by 3. Multiple of 8 will remain the same as original number after this operation. Examples: 8:        1000 >> 3 :  0001 <<3  :  1000 24:      11000… Continue reading Multiples of 8

General Discussions

# Code Gets to Decide

General Discussions

# Check if all Bits Set

To check if all the bits are set for a given number, AND the given number n with n+1. If all the bits are set, then the result will be 0. Examples: n = 4          100 n+1 = 5      101 ———————– AND result         100 n =… Continue reading Check if all Bits Set

General Discussions

# Union-Find Data Structure: Intro

Originally posted on PH Bytes:
A disjoint-set data structure, also called a union–find data structure keeps track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets. With this setting we define the following operations: Find: Determine which subset a particular element is in. This can be used for determining if two…

General Discussions

# Checking Alternate Bits Set

To check if alternate bits are set, Carry out the following operations: First XOR the number n and (n >> 1). If alternate bits are set then this will produce all 1’s. Let us call the result as m To check is all the bits are set to 1, AND m with m + 1… Continue reading Checking Alternate Bits Set