WeixiangYan/CodeTransOcean huggingface dataset:
Task: Percentage difference between images




Go: package main

import (
    "fmt"
    "image/jpeg"
    "os"
    "log"
    "image"
)

func loadJpeg(filename string) (image.Image, error) {
    f, err := os.Open(filename)
    if err != nil {
        return nil, err
    }
    defer f.Close()

    img, err := jpeg.Decode(f)
    if err != nil {
        return nil, err
    }

    return img, nil
}

func diff(a, b uint32) int64 {
    if a > b {
        return int64(a - b)
    }
    return int64(b - a)
}

func main() {
    i50, err := loadJpeg("Lenna50.jpg")
    if err != nil {
        log.Fatal(err)
    }

    i100, err := loadJpeg("Lenna100.jpg")
    if err != nil {
        log.Fatal(err)
    }

    if i50.ColorModel() != i100.ColorModel() {
        log.Fatal("different color models")
    }

    b := i50.Bounds()
    if !b.Eq(i100.Bounds()) {
        log.Fatal("different image sizes")
    }

    var sum int64
    for y := b.Min.Y; y < b.Max.Y; y++ {
        for x := b.Min.X; x < b.Max.X; x++ {
            r1, g1, b1, _ := i50.At(x, y).RGBA()
            r2, g2, b2, _ := i100.At(x, y).RGBA()
            sum += diff(r1, r2)
            sum += diff(g1, g2)
            sum += diff(b1, b2)
        }
    }

    nPixels := (b.Max.X - b.Min.X) * (b.Max.Y - b.Min.Y)
    fmt.Printf("Image difference: %f%%\n",
        float64(sum*100)/(float64(nPixels)*0xffff*3))
}







Fortran: program ImageDifference

  use RCImageBasic
  use RCImageIO

  implicit none

  integer, parameter :: input1_u = 20, &
                        input2_u = 21

  type(rgbimage) :: lenna1, lenna2
  real           :: totaldiff


  open(input1_u, file="Lenna100.ppm", action="read")
  open(input2_u, file="Lenna50.ppm", action="read")
  call read_ppm(input1_u, lenna1)
  call read_ppm(input2_u, lenna2)
  close(input1_u)
  close(input2_u)

  totaldiff = sum(  (abs(lenna1%red - lenna2%red) + &
                     abs(lenna1%green - lenna2%green) + &
                     abs(lenna1%blue - lenna2%blue)) / 255.0 )


  print *, 100.0 * totaldiff / (lenna1%width * lenna1%height * 3.0)

  call free_img(lenna1)
  call free_img(lenna2)

end program ImageDifference
Task: Percentage difference between images





Java: import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public enum ImgDiffPercent {
    ;

    public static void main(String[] args) throws IOException {
        
        
        BufferedImage img1 = ImageIO.read(new File("Lenna50.jpg"));
        BufferedImage img2 = ImageIO.read(new File("Lenna100.jpg"));

        double p = getDifferencePercent(img1, img2);
        System.out.println("diff percent: " + p);
    }

    private static double getDifferencePercent(BufferedImage img1, BufferedImage img2) {
        int width = img1.getWidth();
        int height = img1.getHeight();
        int width2 = img2.getWidth();
        int height2 = img2.getHeight();
        if (width != width2 || height != height2) {
            throw new IllegalArgumentException(String.format("Images must have the same dimensions: (%d,%d) vs. (%d,%d)", width, height, width2, height2));
        }

        long diff = 0;
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                diff += pixelDiff(img1.getRGB(x, y), img2.getRGB(x, y));
            }
        }
        long maxDiff = 3L * 255 * width * height;

        return 100.0 * diff / maxDiff;
    }

    private static int pixelDiff(int rgb1, int rgb2) {
        int r1 = (rgb1 >> 16) & 0xff;
        int g1 = (rgb1 >>  8) & 0xff;
        int b1 =  rgb1        & 0xff;
        int r2 = (rgb2 >> 16) & 0xff;
        int g2 = (rgb2 >>  8) & 0xff;
        int b2 =  rgb2        & 0xff;
        return Math.abs(r1 - r2) + Math.abs(g1 - g2) + Math.abs(b1 - b2);
    }
}






Fortran: program ImageDifference

  use RCImageBasic
  use RCImageIO

  implicit none

  integer, parameter :: input1_u = 20, &
                        input2_u = 21

  type(rgbimage) :: lenna1, lenna2
  real           :: totaldiff


  open(input1_u, file="Lenna100.ppm", action="read")
  open(input2_u, file="Lenna50.ppm", action="read")
  call read_ppm(input1_u, lenna1)
  call read_ppm(input2_u, lenna2)
  close(input1_u)
  close(input2_u)

  totaldiff = sum(  (abs(lenna1%red - lenna2%red) + &
                     abs(lenna1%green - lenna2%green) + &
                     abs(lenna1%blue - lenna2%blue)) / 255.0 )


  print *, 100.0 * totaldiff / (lenna1%width * lenna1%height * 3.0)

  call free_img(lenna1)
  call free_img(lenna2)

end program ImageDifference
Task: Percentage difference between images







Python: from PIL import Image

i1 = Image.open("image1.jpg")
i2 = Image.open("image2.jpg")
assert i1.mode == i2.mode, "Different kinds of images."
assert i1.size == i2.size, "Different sizes."

pairs = zip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
    
    dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
    dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))

ncomponents = i1.size[0] * i1.size[1] * 3
print ("Difference (percentage):", (dif / 255.0 * 100) / ncomponents)




Fortran: program ImageDifference

  use RCImageBasic
  use RCImageIO

  implicit none

  integer, parameter :: input1_u = 20, &
                        input2_u = 21

  type(rgbimage) :: lenna1, lenna2
  real           :: totaldiff


  open(input1_u, file="Lenna100.ppm", action="read")
  open(input2_u, file="Lenna50.ppm", action="read")
  call read_ppm(input1_u, lenna1)
  call read_ppm(input2_u, lenna2)
  close(input1_u)
  close(input2_u)

  totaldiff = sum(  (abs(lenna1%red - lenna2%red) + &
                     abs(lenna1%green - lenna2%green) + &
                     abs(lenna1%blue - lenna2%blue)) / 255.0 )


  print *, 100.0 * totaldiff / (lenna1%width * lenna1%height * 3.0)

  call free_img(lenna1)
  call free_img(lenna2)

end program ImageDifference
Task: Bitwise operations



C#: static void bitwise(int a, int b)
        {
            Console.WriteLine("a and b is {0}", a & b);
            Console.WriteLine("a or b is {0}", a | b);
            Console.WriteLine("a xor b is {0}", a ^ b);
            Console.WriteLine("not a is {0}", ~a);
            Console.WriteLine("a lshift b is {0}", a << b);
            Console.WriteLine("a arshift b is {0}", a >> b); 
                                                             
            uint c = (uint)a;
            Console.WriteLine("c rshift b is {0}", c >> b); 
                                                            
            
        }









Ada: with Ada.Text_IO, Interfaces;
use  Ada.Text_IO, Interfaces;

procedure Bitwise is
   subtype Byte is Unsigned_8;
   package Byte_IO is new Ada.Text_Io.Modular_IO (Byte);

   A : constant Byte    := 2#00011110#;
   B : constant Byte    := 2#11110100#;
   X : constant Byte    := 128;
   N : constant Natural := 1;
begin
   Put ("A and B = ");  Byte_IO.Put (Item => A and B, Base => 2);  New_Line;
   Put ("A or B  = ");  Byte_IO.Put (Item => A or B,  Base => 2);  New_Line;
   Put ("A xor B = ");  Byte_IO.Put (Item => A xor B, Base => 2);  New_Line;
   Put ("not A   = ");  Byte_IO.Put (Item => not A,   Base => 2);  New_Line;
   New_Line (2);
   Put_Line (Unsigned_8'Image (Shift_Left  (X, N)));
   Put_Line (Unsigned_8'Image (Shift_Right (X, N)));
   Put_Line (Unsigned_8'Image (Shift_Right_Arithmetic (X, N)));
   Put_Line (Unsigned_8'Image (Rotate_Left  (X, N)));
   Put_Line (Unsigned_8'Image (Rotate_Right (X, N)));
end Bitwise;
Task: Bitwise operations

C: void bitwise(int a, int b)
{
  printf("a and b: %d\n", a & b);
  printf("a or b: %d\n", a | b);
  printf("a xor b: %d\n", a ^ b);
  printf("not a: %d\n", ~a);
  printf("a << n: %d\n", a << b); 
  printf("a >> n: %d\n", a >> b); 
  
  unsigned int c = a;
  printf("c >> b: %d\n", c >> b); 
  
  return 0;
}











Ada: with Ada.Text_IO, Interfaces;
use  Ada.Text_IO, Interfaces;

procedure Bitwise is
   subtype Byte is Unsigned_8;
   package Byte_IO is new Ada.Text_Io.Modular_IO (Byte);

   A : constant Byte    := 2#00011110#;
   B : constant Byte    := 2#11110100#;
   X : constant Byte    := 128;
   N : constant Natural := 1;
begin
   Put ("A and B = ");  Byte_IO.Put (Item => A and B, Base => 2);  New_Line;
   Put ("A or B  = ");  Byte_IO.Put (Item => A or B,  Base => 2);  New_Line;
   Put ("A xor B = ");  Byte_IO.Put (Item => A xor B, Base => 2);  New_Line;
   Put ("not A   = ");  Byte_IO.Put (Item => not A,   Base => 2);  New_Line;
   New_Line (2);
   Put_Line (Unsigned_8'Image (Shift_Left  (X, N)));
   Put_Line (Unsigned_8'Image (Shift_Right (X, N)));
   Put_Line (Unsigned_8'Image (Shift_Right_Arithmetic (X, N)));
   Put_Line (Unsigned_8'Image (Rotate_Left  (X, N)));
   Put_Line (Unsigned_8'Image (Rotate_Right (X, N)));
end Bitwise;
Task: Bitwise operations


C++: #include <iostream>

void bitwise(int a, int b)
{
  std::cout << "a and b: " << (a & b)  << '\n'; 
  std::cout << "a or b:  " << (a | b)  << '\n';
  std::cout << "a xor b: " << (a ^ b)  << '\n';
  std::cout << "not a:   " << ~a       << '\n';

  
  
  std::cout << "a shl b: " << (a << b) << '\n'; 
  std::cout << "a shr b: " << (a >> b) << '\n'; 
  unsigned int ua = a;
  std::cout << "a lsr b: " << (ua >> b) << '\n'; 

  
  
  
  std::cout << "a rol b: " << std::rotl(ua, b) << '\n';
  std::cout << "a ror b: " << std::rotr(ua, b) << '\n';
  
}










Ada: with Ada.Text_IO, Interfaces;
use  Ada.Text_IO, Interfaces;

procedure Bitwise is
   subtype Byte is Unsigned_8;
   package Byte_IO is new Ada.Text_Io.Modular_IO (Byte);

   A : constant Byte    := 2#00011110#;
   B : constant Byte    := 2#11110100#;
   X : constant Byte    := 128;
   N : constant Natural := 1;
begin
   Put ("A and B = ");  Byte_IO.Put (Item => A and B, Base => 2);  New_Line;
   Put ("A or B  = ");  Byte_IO.Put (Item => A or B,  Base => 2);  New_Line;
   Put ("A xor B = ");  Byte_IO.Put (Item => A xor B, Base => 2);  New_Line;
   Put ("not A   = ");  Byte_IO.Put (Item => not A,   Base => 2);  New_Line;
   New_Line (2);
   Put_Line (Unsigned_8'Image (Shift_Left  (X, N)));
   Put_Line (Unsigned_8'Image (Shift_Right (X, N)));
   Put_Line (Unsigned_8'Image (Shift_Right_Arithmetic (X, N)));
   Put_Line (Unsigned_8'Image (Rotate_Left  (X, N)));
   Put_Line (Unsigned_8'Image (Rotate_Right (X, N)));
end Bitwise;
Task: Bitwise operations




Go: package main

import "fmt"

func bitwise(a, b int16) {
	fmt.Printf("a:   %016b\n", uint16(a))
	fmt.Printf("b:   %016b\n", uint16(b))

	
	fmt.Printf("and: %016b\n", uint16(a&b))
	fmt.Printf("or:  %016b\n", uint16(a|b))
	fmt.Printf("xor: %016b\n", uint16(a^b))
	fmt.Printf("not: %016b\n", uint16(^a))

	if b < 0 {
		fmt.Println("Right operand is negative, but all shifts require an unsigned right operand (shift distance).")
		return
	}
	ua := uint16(a)
	ub := uint32(b)

	
	fmt.Printf("shl: %016b\n", uint16(ua<<ub))
	fmt.Printf("shr: %016b\n", uint16(ua>>ub))

	
	fmt.Printf("las: %016b\n", uint16(a<<ub))
	fmt.Printf("ras: %016b\n", uint16(a>>ub))

	
	fmt.Printf("rol: %016b\n", uint16(a<<ub|int16(uint16(a)>>(16-ub))))
	fmt.Printf("ror: %016b\n", uint16(int16(uint16(a)>>ub)|a<<(16-ub)))
}

func main() {
	var a, b int16 = -460, 6
	bitwise(a, b)
}








Ada: with Ada.Text_IO, Interfaces;
use  Ada.Text_IO, Interfaces;

procedure Bitwise is
   subtype Byte is Unsigned_8;
   package Byte_IO is new Ada.Text_Io.Modular_IO (Byte);

   A : constant Byte    := 2#00011110#;
   B : constant Byte    := 2#11110100#;
   X : constant Byte    := 128;
   N : constant Natural := 1;
begin
   Put ("A and B = ");  Byte_IO.Put (Item => A and B, Base => 2);  New_Line;
   Put ("A or B  = ");  Byte_IO.Put (Item => A or B,  Base => 2);  New_Line;
   Put ("A xor B = ");  Byte_IO.Put (Item => A xor B, Base => 2);  New_Line;
   Put ("not A   = ");  Byte_IO.Put (Item => not A,   Base => 2);  New_Line;
   New_Line (2);
   Put_Line (Unsigned_8'Image (Shift_Left  (X, N)));
   Put_Line (Unsigned_8'Image (Shift_Right (X, N)));
   Put_Line (Unsigned_8'Image (Shift_Right_Arithmetic (X, N)));
   Put_Line (Unsigned_8'Image (Rotate_Left  (X, N)));
   Put_Line (Unsigned_8'Image (Rotate_Right (X, N)));
end Bitwise;
Task: Bitwise operations





Java: module BitwiseOps
    {
    @Inject Console console;
    void run()
        {
        for ((Int64 n1, Int64 n2) : [0=7, 1=5, 42=2, 0x123456789ABCDEF=0xFF])  
            {
            static String hex(Int64 n)   
                {
                
                return n.toByteArray() [(n.leadingZeroCount / 8).minOf(7) ..< 8].toString();
                }

            console.print($|For values {n1} ({hex(n1)}) and {n2} ({hex(n2)}):
                           |  {hex(n1)} AND {hex(n2)} = {hex(n1 & n2)}
                           |  {hex(n1)} OR {hex(n2)} = {hex(n1 | n2)}
                           |  {hex(n1)} XOR {hex(n2)} = {hex(n1 ^ n2)}
                           |  NOT {hex(n1)} = {hex(~n1)}
                           |  left shift {hex(n1)} by {n2} = {hex(n1 << n2)}
                           |  right shift {hex(n1)} by {n2} = {hex(n1 >> n2)}
                           |  right arithmetic shift {hex(n1)} by {n2} = {hex(n1 >>> n2)}
                           |  left rotate {hex(n1)} by {n2} = {hex(n1.rotateLeft(n2))}
                           |  right rotate {hex(n1)} by {n2} = {hex(n1.rotateRight(n2))}
                           |  leftmost bit of {hex(n1)} = {hex(n1.leftmostBit)}
                           |  rightmost bit of {hex(n1)} = {hex(n1.rightmostBit)}
                           |  leading zero count of {hex(n1)} = {n1.leadingZeroCount}
                           |  trailing zero count of {hex(n1)} = {n1.trailingZeroCount}
                           |  bit count (aka "population") of {hex(n1)} = {n1.bitCount}
                           |  reversed bits of {hex(n1)} = {hex(n1.reverseBits())}
                           |  reverse bytes of {hex(n1)} = {hex(n1.reverseBytes())}
                           |
                         );
            }
        }
    }







Ada: with Ada.Text_IO, Interfaces;
use  Ada.Text_IO, Interfaces;

procedure Bitwise is
   subtype Byte is Unsigned_8;
   package Byte_IO is new Ada.Text_Io.Modular_IO (Byte);

   A : constant Byte    := 2#00011110#;
   B : constant Byte    := 2#11110100#;
   X : constant Byte    := 128;
   N : constant Natural := 1;
begin
   Put ("A and B = ");  Byte_IO.Put (Item => A and B, Base => 2);  New_Line;
   Put ("A or B  = ");  Byte_IO.Put (Item => A or B,  Base => 2);  New_Line;
   Put ("A xor B = ");  Byte_IO.Put (Item => A xor B, Base => 2);  New_Line;
   Put ("not A   = ");  Byte_IO.Put (Item => not A,   Base => 2);  New_Line;
   New_Line (2);
   Put_Line (Unsigned_8'Image (Shift_Left  (X, N)));
   Put_Line (Unsigned_8'Image (Shift_Right (X, N)));
   Put_Line (Unsigned_8'Image (Shift_Right_Arithmetic (X, N)));
   Put_Line (Unsigned_8'Image (Rotate_Left  (X, N)));
   Put_Line (Unsigned_8'Image (Rotate_Right (X, N)));
end Bitwise;
Task: Bitwise operations







Python: def bitwise_built_ins(width, a, b):
    mask = (1 << width) - 1
    print(f)

def rotr(width, a, n):
    "Rotate a, n times to the right"
    if n < 0:
        return rotl(width, a, -n)
    elif n == 0:
        return a
    else:
        mask = (1 << width) - 1
        a, n = a & mask, n % width
        return ((a >> n)    
                | ((a & ((1 << n) - 1))   
                   << (width - n)))  

def rotl(width, a, n):
    "Rotate a, n times to the left"
    if n < 0:
        return rotr(width, a, -n)
    elif n == 0:
        return a
    else:
        mask = (1 << width) - 1
        a, n = a & mask, n % width
        return (((a << n) & mask)      
                | (a >> (width - n)))  
    
def asr(width, a, n):
    "Arithmetic shift a, n times to the right. (sign preserving)."
    mask, top_bit_mask = ((1 << width) - 1), 1 << (width - 1)
    if n < 0:
        return  (a << -n) & mask
    elif n == 0:
        return a
    elif n >= width:
        return mask if a & top_bit_mask else 0
    else:
        a = a & mask
        if a & top_bit_mask:    
            signs = (1 << n) - 1
            return a >> n | (signs << width - n)
        else:
            return a >> n
    
      
def helper_funcs(width, a):
    mask, top_bit_mask = ((1 << width) - 1), 1 << (width - 1)
    aa = a | top_bit_mask  
    print(f)

if __name__ == '__main__':
    bitwise_built_ins(8, 27, 125)
    helper_funcs(8, 27)





Ada: with Ada.Text_IO, Interfaces;
use  Ada.Text_IO, Interfaces;

procedure Bitwise is
   subtype Byte is Unsigned_8;
   package Byte_IO is new Ada.Text_Io.Modular_IO (Byte);

   A : constant Byte    := 2#00011110#;
   B : constant Byte    := 2#11110100#;
   X : constant Byte    := 128;
   N : constant Natural := 1;
begin
   Put ("A and B = ");  Byte_IO.Put (Item => A and B, Base => 2);  New_Line;
   Put ("A or B  = ");  Byte_IO.Put (Item => A or B,  Base => 2);  New_Line;
   Put ("A xor B = ");  Byte_IO.Put (Item => A xor B, Base => 2);  New_Line;
   Put ("not A   = ");  Byte_IO.Put (Item => not A,   Base => 2);  New_Line;
   New_Line (2);
   Put_Line (Unsigned_8'Image (Shift_Left  (X, N)));
   Put_Line (Unsigned_8'Image (Shift_Right (X, N)));
   Put_Line (Unsigned_8'Image (Shift_Right_Arithmetic (X, N)));
   Put_Line (Unsigned_8'Image (Rotate_Left  (X, N)));
   Put_Line (Unsigned_8'Image (Rotate_Right (X, N)));
end Bitwise;
Task: Bitwise operations








VB: Debug.Print Hex(&HF0F0 And &HFF00)  
Debug.Print Hex(&HF0F0 Or &HFF00)   
Debug.Print Hex(&HF0F0 Xor &HFF00)  
Debug.Print Hex(Not &HF0F0)         
Debug.Print Hex(&HF0F0 Eqv &HFF00)  
Debug.Print Hex(&HF0F0 Imp &HFF00)  




Ada: with Ada.Text_IO, Interfaces;
use  Ada.Text_IO, Interfaces;

procedure Bitwise is
   subtype Byte is Unsigned_8;
   package Byte_IO is new Ada.Text_Io.Modular_IO (Byte);

   A : constant Byte    := 2#00011110#;
   B : constant Byte    := 2#11110100#;
   X : constant Byte    := 128;
   N : constant Natural := 1;
begin
   Put ("A and B = ");  Byte_IO.Put (Item => A and B, Base => 2);  New_Line;
   Put ("A or B  = ");  Byte_IO.Put (Item => A or B,  Base => 2);  New_Line;
   Put ("A xor B = ");  Byte_IO.Put (Item => A xor B, Base => 2);  New_Line;
   Put ("not A   = ");  Byte_IO.Put (Item => not A,   Base => 2);  New_Line;
   New_Line (2);
   Put_Line (Unsigned_8'Image (Shift_Left  (X, N)));
   Put_Line (Unsigned_8'Image (Shift_Right (X, N)));
   Put_Line (Unsigned_8'Image (Shift_Right_Arithmetic (X, N)));
   Put_Line (Unsigned_8'Image (Rotate_Left  (X, N)));
   Put_Line (Unsigned_8'Image (Rotate_Right (X, N)));
end Bitwise;
Task: Bitwise operations

C: void bitwise(int a, int b)
{
  printf("a and b: %d\n", a & b);
  printf("a or b: %d\n", a | b);
  printf("a xor b: %d\n", a ^ b);
  printf("not a: %d\n", ~a);
  printf("a << n: %d\n", a << b); 
  printf("a >> n: %d\n", a >> b); 
  
  unsigned int c = a;
  printf("c >> b: %d\n", c >> b); 
  
  return 0;
}
Task: Bitwise operations



C#: static void bitwise(int a, int b)
        {
            Console.WriteLine("a and b is {0}", a & b);
            Console.WriteLine("a or b is {0}", a | b);
            Console.WriteLine("a xor b is {0}", a ^ b);
            Console.WriteLine("not a is {0}", ~a);
            Console.WriteLine("a lshift b is {0}", a << b);
            Console.WriteLine("a arshift b is {0}", a >> b); 
                                                             
            uint c = (uint)a;
            Console.WriteLine("c rshift b is {0}", c >> b); 
                                                            
            
        }
Task: Bitwise operations


C++: #include <iostream>

void bitwise(int a, int b)
{
  std::cout << "a and b: " << (a & b)  << '\n'; 
  std::cout << "a or b:  " << (a | b)  << '\n';
  std::cout << "a xor b: " << (a ^ b)  << '\n';
  std::cout << "not a:   " << ~a       << '\n';

  
  
  std::cout << "a shl b: " << (a << b) << '\n'; 
  std::cout << "a shr b: " << (a >> b) << '\n'; 
  unsigned int ua = a;
  std::cout << "a lsr b: " << (ua >> b) << '\n'; 

  
  
  
  std::cout << "a rol b: " << std::rotl(ua, b) << '\n';
  std::cout << "a ror b: " << std::rotr(ua, b) << '\n';
  
}
Task: Bitwise operations





Java: module BitwiseOps
    {
    @Inject Console console;
    void run()
        {
        for ((Int64 n1, Int64 n2) : [0=7, 1=5, 42=2, 0x123456789ABCDEF=0xFF])  
            {
            static String hex(Int64 n)   
                {
                
                return n.toByteArray() [(n.leadingZeroCount / 8).minOf(7) ..< 8].toString();
                }

            console.print($|For values {n1} ({hex(n1)}) and {n2} ({hex(n2)}):
                           |  {hex(n1)} AND {hex(n2)} = {hex(n1 & n2)}
                           |  {hex(n1)} OR {hex(n2)} = {hex(n1 | n2)}
                           |  {hex(n1)} XOR {hex(n2)} = {hex(n1 ^ n2)}
                           |  NOT {hex(n1)} = {hex(~n1)}
                           |  left shift {hex(n1)} by {n2} = {hex(n1 << n2)}
                           |  right shift {hex(n1)} by {n2} = {hex(n1 >> n2)}
                           |  right arithmetic shift {hex(n1)} by {n2} = {hex(n1 >>> n2)}
                           |  left rotate {hex(n1)} by {n2} = {hex(n1.rotateLeft(n2))}
                           |  right rotate {hex(n1)} by {n2} = {hex(n1.rotateRight(n2))}
                           |  leftmost bit of {hex(n1)} = {hex(n1.leftmostBit)}
                           |  rightmost bit of {hex(n1)} = {hex(n1.rightmostBit)}
                           |  leading zero count of {hex(n1)} = {n1.leadingZeroCount}
                           |  trailing zero count of {hex(n1)} = {n1.trailingZeroCount}
                           |  bit count (aka "population") of {hex(n1)} = {n1.bitCount}
                           |  reversed bits of {hex(n1)} = {hex(n1.reverseBits())}
                           |  reverse bytes of {hex(n1)} = {hex(n1.reverseBytes())}
                           |
                         );
            }
        }
    }
Task: Bitwise operations







Python: def bitwise_built_ins(width, a, b):
    mask = (1 << width) - 1
    print(f)

def rotr(width, a, n):
    "Rotate a, n times to the right"
    if n < 0:
        return rotl(width, a, -n)
    elif n == 0:
        return a
    else:
        mask = (1 << width) - 1
        a, n = a & mask, n % width
        return ((a >> n)    
                | ((a & ((1 << n) - 1))   
                   << (width - n)))  

def rotl(width, a, n):
    "Rotate a, n times to the left"
    if n < 0:
        return rotr(width, a, -n)
    elif n == 0:
        return a
    else:
        mask = (1 << width) - 1
        a, n = a & mask, n % width
        return (((a << n) & mask)      
                | (a >> (width - n)))  
    
def asr(width, a, n):
    "Arithmetic shift a, n times to the right. (sign preserving)."
    mask, top_bit_mask = ((1 << width) - 1), 1 << (width - 1)
    if n < 0:
        return  (a << -n) & mask
    elif n == 0:
        return a
    elif n >= width:
        return mask if a & top_bit_mask else 0
    else:
        a = a & mask
        if a & top_bit_mask:    
            signs = (1 << n) - 1
            return a >> n | (signs << width - n)
        else:
            return a >> n
    
      
def helper_funcs(width, a):
    mask, top_bit_mask = ((1 << width) - 1), 1 << (width - 1)
    aa = a | top_bit_mask  
    print(f)

if __name__ == '__main__':
    bitwise_built_ins(8, 27, 125)
    helper_funcs(8, 27)
Task: Bitwise operations








VB: Debug.Print Hex(&HF0F0 And &HFF00)  
Debug.Print Hex(&HF0F0 Or &HFF00)   
Debug.Print Hex(&HF0F0 Xor &HFF00)  
Debug.Print Hex(Not &HF0F0)         
Debug.Print Hex(&HF0F0 Eqv &HFF00)  
Debug.Print Hex(&HF0F0 Imp &HFF00)