Insert M Into N

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This Python challenge is about inserting a 16-bit number m into another 16-bit number n such that m starts at bit j and ends at bit i.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") algorithm/BasicAlgorithmsGroup -.-> algorithm/bit_manipulation("`Bit Manipulation`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") subgraph Lab Skills python/comments -.-> lab-268816{{"`Insert M Into N`"}} algorithm/bit_manipulation -.-> lab-268816{{"`Insert M Into N`"}} python/conditional_statements -.-> lab-268816{{"`Insert M Into N`"}} python/tuples -.-> lab-268816{{"`Insert M Into N`"}} python/function_definition -.-> lab-268816{{"`Insert M Into N`"}} python/classes_objects -.-> lab-268816{{"`Insert M Into N`"}} python/encapsulation -.-> lab-268816{{"`Insert M Into N`"}} python/raising_exceptions -.-> lab-268816{{"`Insert M Into N`"}} end

Insert M Into N

Problem

Given two 16-bit numbers, n and m, and two indices i and j, insert m into n such that m starts at bit j and ends at bit i. The program should handle the following cases:

  • If none is given as an input, an exception should be raised.
  • If a negative index is given for i or j, an exception should be raised.
  • If the inputs are invalid, an exception should be raised.
  • If i through j do not have enough space for m, an exception should be raised.

The program should return the resulting 16-bit number after the insertion.

Requirements

The program should meet the following requirements:

  • j should be greater than i.
  • i through j should have enough space for m.
  • The inputs should be valid.
  • The program should fit in memory.

Example Usage

Here is an example of the program's usage:

i      = 2
j      = 6
n      = 0000 0100 0000 0000
m      = 0000 0000 0001 0011
result = 0000 0100 0100 1100

In this example, m is inserted into n such that m starts at bit j=6 and ends at bit i=2. The resulting 16-bit number is 0000 0100 0100 1100.

Summary

This Python challenge requires the program to insert a 16-bit number m into another 16-bit number n such that m starts at bit j and ends at bit i. The program should handle various cases and meet the given requirements.

Other Algorithm Tutorials you may like