Saturday, 30 August 2025

Tearing bit into bits (C++)


A gist of vital questions/concepts related to Bit wise manipulation : 

  1. Turn off the first set bit(1) from right of a number N.
    1. Application: Fast Bit Count 
    2. Application: Single Number III
  2. Odd/Even test
  3. Get Kth bit of N / Checking if the Kth bit is set or unset: { bool bit_K = num & (1 << K); }
  4. Set Kth bit of N:  {num |= (1 << pos);}
  5. Unset Kth bit of N: {num &= (~(1 << pos));}
  6. Toggling a bit at Kth position: {num ^= (1 << pos);}
  7. Convert an integer (Decimal) into a bitwise (string) 
  8. Difference between '&' and '&&' or for that matter '|' and '||'
  9. Range of (a &b) , range of(a|b), range of (a^b) 

REMEBER the tools at our disposal are : 

  • SHIFT operators : >>(right shift, div by 2) and <<(left shift, mul by 2)
  • BOOL operators : ^ (XOR), &(and) , ~(NOT)
  • Arithmetic operators like +, - , /, * are not used. 

int int_var;
cout<<sizeof(int_var)<<endl; // returns number of bytes

output is  4 
this means an integer has 32 bits or 32 placeholders to hold either 1 or 0. 

1.Turn off the first set bit(1) of a number N.

Two ways : 

A. 

  • get ~N 
  • add +1
  • multiply this with N... log of this result will show the position of rightmost set bit
B
  • get (N-1)
  • flip the bit ~(N-1)
  • multiply this with N... log of this result will show the position of rightmost set bit
Fast Bit Count : 

```Java
count=0;
while(n>0) {
    n= n &(n-1);
    count++;
}
return count;
```

2. Check whether n is even or odd: The naive approach to check a number is even or odd is to take the modulo with 2. The better and efficient method is to take the (n&1). If the last bit is set n is odd, else even.

3. If you want the k-th bit of n, then do : 

(n & ( 1 << k )) >> k

(n >> k) & 1 is equally valid 


5. 
A&B =[0,min(A,B)] A^B =[0,A+B] A|B =[max(A,B),A+B]




TIPS : 
  •  mask = 0xffffffff  then ~(a^mask)


References : 


Sunday, 27 April 2025

[Graph] - DAG : An important observation

In a DAG, a mutually unreachable pair (s,t) can exist if and only if there exists at least one topological level with at least two nodes

Read the post at Notion : https://www.notion.so/DAG-Directed-Acyclic-Graph-1d8d32fa6ba880e7aea2d3eb59ad46e1

Sunday, 16 March 2025

Leetcode Weekly Contest 441 Q2. Closest Equal Element Queries

Q2. Closest Equal Element Queries

Important learnings wrt ๐Ÿ”Circular Array๐Ÿ”  : 

 The two distances between elt at i & j  : 

  • forward_dist  = (j-i+n) % n;
  • backward_dist = (i-j+n) % n; 
How to get the two indexes `d` distance away from a given index, i ?
  • next_d_idx = ( i + d) % n ;
  • prev_d_idx = ( i - d + n) % n ;
As a special case when d=1, we can get next & prev elements as : 
  • next_idx = (i+1) % n 
  • prev_idx = (i-1+n ) % n
Alternatively, the +1 for forward should be thought as +(n-1) for backward or more generally, +d should be thought as +(n-d) when marching in backward direction.

Sunday, 9 March 2025

"Mastering Power Set Generation: A Deep Dive into Recursion” from a Java ☕ programmer’s perspective

Motivation for the notes :

  1. Leetcode#78. Subsets 
  2. G4G Power Set 

There are broadly two approaches :

  1. Bit Masking (iterative) ๐Ÿ˜ท{not focus of this blog}
  2. Backtracking (recursive) ⭐
    1. Include-Exclude paradigm : here we see two variants & why we should use one over other
    2. For-loop paradigm : prefer this (reason at the end)

Continue reading my latest blog compiled at notion.so by clicking here :  "Mastering Power Set Generation: A Deep Dive into Recursion” from a Java ☕ programmer’s perspective



Friday, 22 March 2024

SUNY Buffalo : MS Tracks and Specializations (Graphic Representation)

Graduate degree options provided by Departement of CSE at SUNY Buffalo can be graphically represented as : 

 


(Click on the images for enlarged view ๐Ÿ”)

The graphic representation of the semester wise breakdown of programm can be depicted as : 




Please comment below if you find anything wrong in the representations. Or, if you want to add more in this.