Bitwise operations: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 99: Line 99:
cr ." a xor b = " over xor .
cr ." a xor b = " over xor .
cr ." not a = " invert . ;
cr ." not a = " invert . ;

=={{header|Fortran}}==


=={{header|Haskell}}==
=={{header|Haskell}}==

Revision as of 02:59, 5 April 2008

Task
Bitwise operations
You are encouraged to solve this task according to the task description, using any language you may know.

Basic Data Operation
This is a basic data operation. It represents a fundamental action on a basic data type.

You may see other such operations in the Basic Data Operations category, or:

Integer Operations
Arithmetic | Comparison

Boolean Operations
Bitwise | Logical

String Operations
Concatenation | Interpolation | Comparison | Matching

Memory Operations
Pointers & references | Addresses


Write a function to perform a bitwise AND, OR, and XOR on two integers and perform a bitwise NOT on the first number.

Ada

The following program performs all required operations and prints the resulting values in base 2 for easy checking of the bit values.

<ada> with Ada.Text_Io; use Ada.Text_Io;

procedure Bitwise is
   type Byte is mod 2**8;
   package Byte_Io is new Ada.Text_Io.Modular_Io(Byte);
   
   A : Byte := 255;
    B : Byte := 170;
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;
end bitwise;</ada>

The output of this program is:

A and B = 2#10101010#
A or B  = 2#11111111#
A xor B = 2#1010101#
Not A   = 2#0#

ALGOL 68

PROC bitwise = (BITS a, BITS b)VOID:
(
  printf(($"a and b: "gl$, a AND b));
  printf(($"a or b: "gl$, a OR b));
  printf(($"a xor b: "gl$, a XOR b));
  printf(($"not a: "gl$, NOT a))
);
bitwise(BIN 255, BIN 170)

The output of this program is:

a and b: FFFFFFFFFFFFFFFFFFFFFFFFTFTFTFTF
a or b: FFFFFFFFFFFFFFFFFFFFFFFFTTTTTTTT
a xor b: FFFFFFFFFFFFFFFFFFFFFFFFFTFTFTFT
not a: TTTTTTTTTTTTTTTTTTTTTTTTFFFFFFFF

BASIC

Works with: QuickBasic version 4.5
SUB bitwise (a, b)
  PRINT a AND b
  PRINT a OR b
  PRINT a XOR b
  PRINT NOT a
END SUB

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);
}
main(){bitwise(255,170);}

The output of this program on a typical platform is:

a and b: 170
a or b: 255
a xor b: 85
not a: -256

D

module bitwise ;
import std.stdio ;

void testbit(int a, int b) {
  writefln("Input:  a = %3d , b = %3d", a, b) ;
  writefln("AND  : %8b & %08b = %032b (%4d)",  a, b, a & b, a & b) ;
  writefln(" OR  : %8b | %08b = %032b (%4d)",  a, b, a | b, a | b) ;
  writefln("XOR  : %8b ^ %08b = %032b (%4d)",  a, b, a ^ b, a ^ b) ;
  writefln("NOT  : %8s ~ %08b = %032b (%4d)", "", a, ~a, ~a) ;
}

void main() {
  int a = 0b11111111 ; // bit literal 255
  int b = 0b10101010 ; // bit literal 170
  
  testbit(a,b) ;  
}

Output:

Input:  a = 255 , b = 170
AND  : 11111111 & 10101010 = 00000000000000000000000010101010 ( 170)
 OR  : 11111111 | 10101010 = 00000000000000000000000011111111 ( 255)
XOR  : 11111111 ^ 10101010 = 00000000000000000000000001010101 (  85)
NOT  :          ~ 11111111 = 11111111111111111111111100000000 (-256)

Forth

: bitwise ( a b -- )
  cr ." a = " over . ." b = " dup .
  cr ." a and b = " 2dup and .
  cr ." a  or b = " 2dup  or .
  cr ." a xor b = " over xor .
  cr ." not a = " invert . ;

Fortran

Haskell

The operations in Data.Bits work on Int, Integer, and any of the sized integer and word types.

import Data.Bits

a = 255
b = 170

a_and_b = a .&. b
a_or_b  = a .|. b
a_xor_b = a `xor` b
not_a   = complement a

J

Actual bitwise calculation occurs in bwo. Other definitions provide formatting.

bwo     =: 17 b. , 23 b. , 22 b. , 28. b.
bwotxt  =: title"_ combine bwo
combine =: [ ,. ":@|:@,:@]
title   =: >;._2 'Bitwise x AND y: |Bitwise x OR y:|Bitwise x XOR y:|Bitwise NOT-x:|'
f       =: (32#2)&#: { '.x'"_

Examples of execution, with output:

   255 bwo 170        NB. numeric output
170 255 85 _256
   255 bwotxt 170     NB. formatted text with English titling
Bitwise x AND y:  170
Bitwise x OR y:   255
Bitwise x XOR y:   85
Bitwise NOT-x:   _256
   f 255 bwo 170      NB. 32-bit diagram
........................x.x.x.x.
........................xxxxxxxx
.........................x.x.x.x
xxxxxxxxxxxxxxxxxxxxxxxx........

Java

public static void bitwise(int a, int b){
  System.out.println("a AND b: " + (a & b));
  System.out.println("a OR b: "+ (a | b));
  System.out.println("a XOR b: "+ (a ^ b));
  System.out.println("NOT a: " + ~a);
}

The call:

bitwise(4, 7);

will output:

a AND b: 4
a OR b: 7
a XOR b: 3
NOT a: -5

to bitwise :a :b
  (print [a and b:] BitAnd :a :b)
  (print [a or b:] BitOr :a :b)
  (print [a xor b:] BitXor :a :b)
  (print [not a:] BitNot :a)
end
bitwise 255 170

The output of this program is:

a and b: 170
a or b: 255 
a xor b: 85
not a: -256

LSE64

over : 2 pick
2dup : over over

bitwise : \
  " A=" ,t over ,h sp " B=" ,t dup ,h nl \
  " A and B=" ,t 2dup & ,h nl \
  " A  or B=" ,t 2dup | ,h nl \
  " A xor B=" ,t over ^ ,h nl \
  " not A="  ,t      ~ ,h nl

\ a \ 7 bitwise   # hex literals

MAXScript

fn bitwise a b =
(
    format "a and b: %\n" (bit.and a b)
    format "a or b: %\n" (bit.or a b)
    format "a xor b: %\n" (bit.xor a b)
    format "not a: %\n" (bit.not a)
)

bitwise 255 170

OCaml

let bitwise a b =
  Printf.printf "a and b: %d\n" (a land b);
  Printf.printf "a or b: %d\n" (a lor b);
  Printf.printf "a xor b: %d\n" (a lxor b);
  Printf.printf "not a: %d\n" (lnot a)

let _ =
  bitwise 255 170

The output of this program on a typical platform is:

a and b: 170
a or b: 255
a xor b: 85
not a: -256

Perl

use integer;

sub bitwise($$) {
  ($a, $b) = @_;
  print 'a and b: '. ($a & $b) ."\n";
  print 'a or b: '.  ($a | $b) ."\n";
  print 'a xor b: '. ($a ^ $b) ."\n";
  print 'not a: '.   (~$a)     ."\n";
}

bitwise(255, 170);

Output:

a and b: 170
a or b: 255
a xor b: 85
not a: -256

Python

def bitwise(a, b):
	print 'a and b: ' + str(a & b)
	print 'a or b: '  + str(a | b)
	print 'a xor b: ' + str(a ^ b)
	print 'not a: '   + str(~a)

bitwise(255,170)

The output of this program is:

a and b: 170
a or b: 255
a xor b: 85
not a: -256

Smalltalk

Since GNU Smalltalk by default runs without a graphical user interface, I wrote the program in that dialect. The actual methods for bitwise operations (bitAnd:, etc.) are the same in all implementations.

a := stdin nextLine asInteger.
b := stdin nextLine asInteger.
('a and b: %1' % {a bitAnd: b}) displayNl.
('a or b: %1' % {a bitOr: b}) displayNl.
('a xor b: %1' % {a bitXor: b}) displayNl.
('not a: %1' % {a bitInvert}) displayNl.