How you debug a binary format

I would like to be able to debug building a binary builder. Right now I am basically printing out the input data to the binary parser, and then going deep into the code and printing out the mapping of the input to the output, then taking the output mapping (integers) and using that to locate the corresponding integer in the binary. Pretty clunky, and requires that I modify the source code deeply to get at the mapping between input and output.

It What seems like you could do is view the binary in different variants (in my case I’d like to view it in 8-bit chunks as decimal numbers, because that’s pretty close to the input). Actually, some numbers are 16 bit, some 8, some 32, etc. So maybe there would be a way to view the binary with each of these different numbers highlighted in memory in some way.

The only way I could see that being possible is if you actually build a visualizer specific to the actual binary format/layout. So it knows where in the sequence the 32 bit numbers should be, and where the 8 bit numbers should be, etc. This is a lot of work and kind of tricky in some situations. So wondering if there’s a general way to do it.

Also wondering what the general way of debugging this type of thing currently is, so maybe I can get some ideas on what to try from that.

Binary Floating Point Range/Precision for 4-bit Mantissa and 4-bit Exponent

I’m trying to understand binary floating point and using just a 4-bit mantissa and a 4-bit exponent (both 2s compliment) to keep things simple.

As far as I can tell, the largest denary number I can represent is 112: 0111 0111

So why do I have trouble representing denary 11 (eleven) in this system? I get that the largest (positive) mantissa is 7 in denary (0111). So I’m inferring that I can’t represent consecutive positive integers between 0 and the maximum value of 112.

Is that correct? If so, can someone help me to see why please? Which numbers in this range can I represent?

Determinants of binary matrices

I was surprised to find that most $ 3\times 3$ matrices with entries in $ \{0,1\}$ have determinant $ 0$ or $ \pm 1$ . There are only six out of 512 matrices with a different determinant (three with $ 2$ and three with $ -2$ ) and these are $ $ \begin{bmatrix}1 & 1 & 0\ 0 & 1 & 1\ 1 & 0 & 1\end{bmatrix} $ $ and the different permutation of this.

Hadamard’s maximum determinant problem for $ \{0,1\}$ asks about the largest possible determinant for matrices with entries in $ \{0,1\}$ and it is known that the sequence of the maximal determinant for $ n\times n$ matrices for $ n=1,2,\dots$ starts with 1, 1, 2, 3, 5, 9, 32, 56, 144, 320,1458 (https://oeis.org/A003432). It is even known that the number of different matrices realizing the maximum (not by absolute value) is 1, 3, 3, 60, 3600, 529200, 75600, 195955200, (https://oeis.org/A051752).

My question is: what is the distribution of determinants of all $ n\times n$ $ \{0,1\}$ -matrices?

Here is the data for (very) small $ n$ : $ $ \begin{array}{lccccccc} n & -3 & -2 & -1 & 0 & 1 & 2 & 3\\hline 1 & & & & 1 & & & \ 2 & & & 3 &10 & 3 & & \ 3 & & 3 & 84 & 338 & 84 & 3 & \ 4 & 60 & 1200 & 10020 & 42976 & 10020 & 1200 & 60 \end{array} $ $

How do I convert an image in a np array to the same format as reading that image using binary read

I’m using the Face cognitive service from Microsoft and my workflow has the image as a numpy array

MS allow images to be passed in as a url or as data in the header

If data is passed in the header, it can be created with a binary read as follows:

# cropped is the image as a numpy array # the three attempts below do not work # as microsoft does not recognise the result # as an image image_data = cropped.tobytes() image_data = cropped.tobytes("F") image_data  = cropped.tobytes("C")  # the following method does work but seems  # a bit ridiculous cv2.imwrite("temp.png", cropped) with open(path_to_image, 'rb') as f:     image_data = f.read() 

I can get my numpy array in the correct format by saving to disk with opencv imwrite and then reading it back in as above, but that doesn’t seem like a sensible thing to do.

I tried using the numpy function tobytes(“F”) and tobytes(“C”) but MS doesn’t recognise the result as a valid image

How can I use numpy to turn my image array into the same format as if I’d read the image from disk?

How to transform a Infinite decimal of $\frac{23}{6}$ into binary?

How to transform a decimal of $ \frac{23}{6}$ into binary?

$ \frac{23}{6}$ is equal to $ 3.83333\bar{33}$ , where the digit $ 3$ s are repeated forever.

I do know how to translate finite decimal into binary, but is there any procedure to translate such Infinite decimal like $ \frac{23}{6}$ into binary?

Any hint is appreciated!

Increment and Decrement on binary semaphores when their values are $1$ and $0$ respectively?

What happens if we increment binary semaphore $ x$ when it is equal to $ 1$ . E.g A process executes $ V(x)$ when $ x==1$ ; What are the possible cases then-

  • Process would be blocked till $ x$ becomes $ 0$ . The moment $ x$ becomes $ 0$ ,it is incremented.

  • Process would be blocked and no present or future changes will be there for $ x$ . i.e Process assumes as if there was never a $ V(x)$ .

  • Other. (If there is please mention).

Same query for $ P(x)$ operation as well when $ x=0$ .


  • Actually, Galvin doesn’t mention in pseudo code what to do during such condition.
  • Better if Someone could provide standard code for $ P(x)$ and $ V(x)$ … Also, $ P$ is wait operation whereas $ V$ is signal operation.

C# binary heap priority queue

I made an A* pathfinder and it was too slow so i did some research into priority queues, i got it working but i am wondering if there is any improvements i can make for this class.

I opted to make a constructor that allows the option of min or max order, since it runs on the pathfinder the performance of this is quite crucial as well. So hopefully some one has some suggestions on ways to improve it.

Here is the class:

public class Heap : IEnumerable {     private readonly bool _isMinHeap;     public readonly IMinHeap[] _items;     private int _count;     public int Size => _count;     public Heap(int size, bool isMinHeap = true)     {         _items = new IMinHeap[size];         _count = 0;         _isMinHeap = isMinHeap;     }      public void Insert(IMinHeap item, int priority)     {         item.SetPriority(priority);         if (Contains(item))         {             UpdateItem(item);             return;         }         _items[_count] = item;         item.HeapIndex = _count;         _count++;         CascadeUp(item);     }      private void UpdateItem(IMinHeap item)     {         int parentIndex = item.HeapIndex / 2;          if (parentIndex > 0 && !HasCorrectParent(item.Value,_items[parentIndex].Value))         {             CascadeUp(item);         }         else         {             CascadeDown(item);         }     }     public bool Contains(IMinHeap item)     {         if(item.HeapIndex < 0 || item.HeapIndex > _items.Length-1 || !Equals(_items[item.HeapIndex], item)) return false;         return true;     }     /// <summary>     /// Clear the priority queue and reset all nodes     /// </summary>     public void Wipe()     {         for (int i = 0; i < _items.Length; i++)         {             _items[i].HeapIndex = -1;         }          Array.Clear(_items,0,_items.Length);         _count = 0;     }     /// <summary>     /// Clear the priority but does not reset node indexes     /// </summary>     public void Clear()     {         Array.Clear(_items, 0, _items.Length);         _count = 0;     }      public bool Pop(out IMinHeap item)     {         item = null;         if (_count == 0)             return false;          item = _items[0];          IMinHeap newRoot = _items[_count];         _items[0] = newRoot;         newRoot.HeapIndex = 0;          _items[_count] = null;         _count--;          CascadeDown(newRoot);          return true;      }     private void CascadeDown(IMinHeap item)     {         do         {             int childLeftIndex = item.HeapIndex * 2;             int childRightIndex = childLeftIndex + 1;              if (!HasCorrectChild(item.Value, _items[childLeftIndex]))             {                 Swap(item.HeapIndex,childLeftIndex);             }             else if (!HasCorrectChild(item.Value, _items[childRightIndex]))             {                 Swap(item.HeapIndex, childLeftIndex);             }              int parentIndex = item.HeapIndex / 2;             SortChildren(parentIndex);              if (!HasCorrectParent(item.Value, _items[parentIndex].Value))             {                 Swap(item.HeapIndex, parentIndex);             }             else             {                 return;             }         } while (true);     }      private bool HasCorrectChild(int itemValue, IMinHeap child)     {         return _isMinHeap && child.Value >= itemValue || !_isMinHeap && child.Value <= itemValue;     }      private void CascadeUp(IMinHeap item)     {         if (item.HeapIndex == 0) return; // there is no parents to look for         do         {             int parentIndex = item.HeapIndex / 2;              if (_items[parentIndex] == null) return;             var parentValue = _items[parentIndex].Value;              if (!HasCorrectParent(item.Value, parentValue))             {                 Swap(item.HeapIndex, parentIndex);                 SortChildren(parentIndex);             }             else             {                 return;             }         } while (true);     }      private void SortChildren(int parentIndex)     {         int leftChildIndex = 2 * parentIndex;         int rightChildIndex = leftChildIndex + 1;          var leftChild = _items[leftChildIndex];         var rightChild = _items[rightChildIndex];          if(leftChild == null || rightChild == null) //the parent has only one or no children - nothing to sort             return;          else if(leftChild.Value > rightChild.Value && _isMinHeap || leftChild.Value < rightChild.Value && !_isMinHeap)             Swap(leftChildIndex,rightChildIndex);     }      private void Swap(int itemIndexA, int itemIndexB)     {         var item = _items[itemIndexA];         var parent = _items[itemIndexB];          _items[itemIndexA] = parent;         _items[itemIndexB] = item;          parent.HeapIndex = itemIndexA;         item.HeapIndex = itemIndexB;     }      private bool HasCorrectParent(int itemValue, int parentValue)     {         return _isMinHeap && parentValue <= itemValue || !_isMinHeap && parentValue >= itemValue;     }      public IEnumerator<IMinHeap> GetEnumerator()     {         for (int i = 0; i < _count; i++)             yield return _items[i];     }      IEnumerator IEnumerable.GetEnumerator()     {         return GetEnumerator();     } } 

Binary tree for 2 elements

I want to understand Binary Search for 2 element list made of 1,2. I draw a tree as below. Is it correct?

enter image description here

If I want to search for an element 2, it will make 2 comparisons. If I want to search for an element 22, it will also make 2 comparisons. But according to formula Θ(lg n), with n=2, it should make only one comparison in worst case. I’m not able to connect these 2 facts. Please help me. (For any even number of elements, I’m not able to draw tree. Because at some point, left side has one node and right side has 2 nodes)

Generic implementation of binary search tree in java

The following is my binary search implementation in Java.

package com.solo.workouts.collections.Tree;  import com.solo.workouts.Implementors.Util; import java.util.Comparator; import java.util.NoSuchElementException; import java.util.Objects;  /*    @author soloworld     @since 1.o   */ public class BinarySearchTree<T> {     private Comparator comparator;     private  Node<T> root;      private BinarySearchTree(Comparator comparator , T type) {          this.comparator = comparator;      }      private BinarySearchTree(Comparator comparator) {         this(comparator,null);     }      public static BinarySearchTree plantTree(Comparator comparator)      {         Objects.requireNonNull(comparator ,"requires an comparator . compartor canot be null");     return new BinarySearchTree(comparator);     }       public void add(T element){         Node<T> node =null;         if(root==null)         {           node = new Node<>(element);           root = node;          }          else  {              Node<T> currentnode = root;              while(currentnode !=null) {                 int result = compare(currentnode, new Node<>(element));                  if (result <= 0) {                     if (currentnode.leftNode == null) {                           currentnode.leftNode = new Node<>(element);                         currentnode = null;                     } else                         currentnode = currentnode.leftNode;                 } else {                     if (currentnode.rightNode == null) {                         currentnode.rightNode = new Node<>(element);                         currentnode = null;                     } else                         currentnode = currentnode.rightNode;                 }              }         }      }      public boolean containsElement(Object e){     Objects.requireNonNull(e);     Node<T> currentnode = new Node<>((T) e);      return contains(currentnode);     }      private boolean contains(Node<T> node) {         Objects.requireNonNull(node);          Node<T> searchnode=null;         Node<T> currentnode =root;          while(searchnode==null &&                 currentnode!=null) {             int value = compare(currentnode, node);              if(value ==0)                 searchnode = currentnode;              else if(value<0)                 currentnode = currentnode.leftNode;              else                 currentnode = currentnode.rightNode;          }          return searchnode !=null;     }      private int compare(Node<T> currentnode, Node<T> element) {         Objects.requireNonNull(currentnode);         Objects.requireNonNull(element);         Objects.requireNonNull(comparator);          return comparator.compare(element.element,currentnode.element);      }      private Node<T> getnode(T element) {         Objects.requireNonNull(element);         Node<T> node = new Node<>(element,null,null);         Node<T> currentnode = root;         while (currentnode!=null && 0!= compare(currentnode,node)) {             int value =  compare(currentnode,node);                if(value <0) {                  currentnode = currentnode.leftNode;             }else {                 currentnode = currentnode.rightNode;             }           }          return currentnode;     }       private <T> Node<T>  getpredecessoe(Node<T> node) {         Node<T> predecessor = null;         return predecessor;     }      public T getsuccessor(T element) {         Objects.requireNonNull(element);         var node = getnode(element);         var successor = getsuccessor(node);         if(successor!=null)              return successor.element;          return null;       }      private  Node<T> getsuccessor(Node<T> node) {         Node<T> successor = root;         if(node.rightNode!=null) {             successor = node.rightNode;              while (successor.leftNode != null) {                 successor = successor.leftNode;             }          }         else {             successor = null;             Node<T> cureentnode = root;             while (cureentnode!= null &&0!= compare(cureentnode,node) ) {                 int compare = compare(cureentnode,node);                   if(compare <=0){                     successor = cureentnode;                     cureentnode = cureentnode.leftNode;                 }                  else                     cureentnode = cureentnode.rightNode;             }          }         return successor;     }        public boolean deleteNode(T element) {         var node =getnode(element);         if(node== null)               return false;          if(node.leftNode ==null && node.rightNode == null)             unlinkNode(node);          else if(node.leftNode!=null && node.rightNode!=null) {            var parent = getParent(node);             var successor = getsuccessor(node);             unlinkSuccessor(successor);             successor.leftNode = node.leftNode;             successor.rightNode = node.rightNode;             linkSuccessorWithParent(parent,successor);         }        else {            var parentNode = getParent(node);            var childNode = node.leftNode!= null ? node.leftNode : node.rightNode;            int compare = compare(parentNode,childNode);             if(compare <=0)                parentNode.leftNode = childNode;             else                parentNode.rightNode  = childNode;              node = null;          }       return node == null;     }      private void linkSuccessorWithParent(Node<T> parent, Node<T> successor) {        int compare =  compare(parent,successor);         if(compare<=0)            parent.leftNode = successor;        else            parent.rightNode = successor;     }      public void unlinkNode(Node<T> node) {       var parent = getParent(node);        Objects.requireNonNull(parent);       Objects.requireNonNull(node);        int compare = compare(parent,node);        if(compare <=0)           parent.leftNode = null;       else           parent.rightNode = null;       }      private  void unlinkSuccessor(final Node<T> successor) {         var parent = getParent(successor);         unlinkNode( successor);          if(successor.rightNode != null)             parent.leftNode = successor.rightNode;       }      private Node<T> getParent(Node<T> childnode) {         var parentnode = root;         var currentnode = root;         Objects.requireNonNull(childnode);         while (0!= compare(currentnode,childnode)) {             parentnode = currentnode;             int compare = compare(currentnode,childnode);              if(compare<0)                 currentnode = currentnode.leftNode;              else if(compare >0)                 currentnode = currentnode.rightNode;         }          return parentnode;     }        public void morrisTreewalk() {      }       private static class Node<T> {         T element;         Node<T> leftNode ;         Node<T> rightNode;          Node(T element, Node<T> leftNode, Node<T> rightNode) {             this.element = element;             this.leftNode = leftNode;             this.rightNode = rightNode;         }          Node(T element) {             this(element,null,null);         }      }   } 

Even though this data structure passes basic test case I was not sure what will be the performance if the amount of input is high. Also if there is any optimisation please tell.