How to resolve the algorithm Bitwise operations step by step in the Octave programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Bitwise operations step by step in the Octave programming language

Table of Contents

Problem Statement

Write a routine to perform a bitwise AND, OR, and XOR on two integers, a bitwise NOT on the first integer, a left shift, right shift, right arithmetic shift, left rotate, and right rotate. All shifts and rotates should be done on the first integer with a shift/rotate amount of the second integer. If any operation is not available in your language, note it.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Bitwise operations step by step in the Octave programming language

Source code in the octave programming language

function bitops(a, b)
  s = sprintf("%s %%s %s = %%s\n", dec2bin(a), dec2bin(b));
  printf(s, "or", dec2bin(bitor(a, b)));
  printf(s, "and", dec2bin(bitand(a, b)));
  printf(s, "xor", dec2bin(bitxor(a, b)));
  printf(s, "left shift", dec2bin(bitshift(a, abs(b))));
  printf(s, "right shift", dec2bin(bitshift(a, -abs(b))));
  printf("simul not %s = %s", dec2bin(a), dec2bin(bitxor(a, 0xffffffff)));
endfunction

bitops(0x1e, 0x3);


  

You may also check:How to resolve the algorithm Hello world/Line printer step by step in the Perl programming language
You may also check:How to resolve the algorithm Sum of a series step by step in the Wren programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the CMake programming language
You may also check:How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n1, continued fraction n2) step by step in the Owl Lisp programming language
You may also check:How to resolve the algorithm First power of 2 that has leading decimal digits of 12 step by step in the Wren programming language