Pages1

Showing posts with label Bitwise Programming. Show all posts
Showing posts with label Bitwise Programming. Show all posts

Rounding up a number to power of 2 < number

Using concept of "n&(n-1)" makes value = 0 if n is number with power of 2

#include <stdio.h>
int nextPwrOf2(int num)
{
     do
     {
 

Rounding up a number to next power of 2.

By using the concept of filling all the bits by 1 using shift operation. 
---------------------------------------------------------------------
#include<stdio.h>
int main()
{
      unsigned int n =1;
      n |= n >> 1;
      n |= n >> 2;