Here we shall discuss some high level Bit hacks that if used cleverly can really speed up your program execution time. We discussed some of the basic hacks in this post:- Low level bit hacks you must know. Please go through the post to get a little understanding of how the things work. Smart learners are anyways welcome to proceed. ;-) BIT HACK 6: Turn off the rightmost 1-bit. Now it finally gets more interesting!!! Bit hacks #1 – #5 were kind of boring to be honest. This bit hack…
Trivia
Some general stuff
-
-
Here we shall discuss some very low level Bit hacks that can really speed up your program execution time when used effectively. Every now and then we see bit level operators. Bit hacks are ingenious little programming tricks that manipulate integers in a smart and efficient manner. Instead of performing some operation (such as counting the 1 bits in an integer) by looping over individual bits, these programming nuggets do the same with one or two carefully chosen bitwise operations. To get things going I’ll assume that you know what…
-
What are enums actually? Enums provide an opportunity to the user to define a shorthand for fixed set of constants like months in a year, gender etc. Enumeration is the list of constant integer values, which means that the enum values can’t be modified. Here we are going to explain Enumeration usage rules and constraints in C language. In C Enums are defined by enum keyword. The enum definition is analogous to the way a structure is defined . Syntax: tagname – It is the name given to the enum…
-
In C, struct keyword must be used for declaring structure variables, but it is optional in C++. For example, following program gives error in C and works in C++. And following program works in both C and C++.
-
We have been discussing the time complexities of each of the algorithm that we have discussed. Let us try to analyze, how these time complexities are calculated. O(1) Time complexity of a function (or set of statements) is considered as O(1) if it doesn’t contain loop, recursion and call to any other non-constant time function. For example swap() function(which interchanges two numbers) has O(1) time complexity. A loop or recursion that runs a constant number of times is also considered as O(1). For example the following loop is O(1). O(n)…
-
Question: Given a sequence of integers, find the median of the integers? Input: arr [] = {15, 23, 4, 16, 8, 42}. Find median. Output: 15 Since the question does not specify anything we cannot assume that the given array is a sorted one. The definition of median is the (n/2)th element of a sorted sequence of integers. This definition makes our approach simpler. All we need to do is sort the integers and then return the (n/2)th element. Here is the code for above approach. I use quick sort…
-
A Programming Contest is a delightful playground for the exploration of intelligence of programmers. To start solving problems in contests, first of all, you have to fix your aim. Some contestants want to increase the number of problems solved by them and the other contestants want to solve less problems but with more efficiency. Choose any of the two categories and then start. If you are a beginner, first try to find the easier problems.Try to solve them within short time. At first, you may need more and more time…
-
Question: What is a wild pointer in a program? Uninitialized pointers are known as wild pointers. These are called so because they point to some arbitrary memory location and may cause a program to crash or behave badly. This can be understood from the below examples. Please note that if a pointer p points to a known variable then it’s not a wild pointer. In the below program, p is a wild pointer till this points to a. If we want pointer to a value (or set of values) without…
-
Question:What is a memory leak in a program? We have been writing many programs where we have used the “free()” keyword. One such example can be found in the “Delete the complete list” function found in the post – Important traversals in a Linked List. The code snippet is:- This is done due to a reason. When we allocate a memory in a program, usually that memory is unallocated once the execution of the program finishes. However, there may be some cases where the program runs forever and these scenarios,…
-
Insertion sort is the most generic example of a sorting algorithm, and the one which is used by one of the most naive users. Often we must have also used this algorithm in our real life examples unknowingly. One of the favorite example is:- This is a very common scenario and we must have used it. Suppose you get a hand of cards. Our general approach is that we start scanning the cards from starting and if we find a card out of place, we remove it from there and…