C Codes

Count Set Bits

C Program to count the set bits:

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

General Discussions

Consider the following: and, As we can clearly see, XOR simulates binary addition without the carry over to the next digit.

General Discussions

Fibonacci Versions – C

Originally posted on PH Bytes:
This post covers the three implementation(Iterative, Recursive and Dynamic) version of Fibonacci series. The correctness of the code covers for integer range numbers only. 1. Iterative Version #include int fibo(int n) { int previous = 1; int current = 1; int next = 1; int i; for (i = 3;…

General Discussions

100000 Hits

Originally posted on Its PH:
Those numbers sure call for a ‘Happy Dance’. The blogging journey began on 16 July 2014 with the post Is this Life All About? – A New Old. The blog today has 488 posts, 101 pages, 12,501 comments and 3434 followers. Thank you, you kind fellow bloggers and friends. 100000 hits…