Welcome, guest | Sign In | My Account | Store | Cart

A collection of simple bit manipulation functions in pure TCL. Functions supplied: testbit setbit clearbit

Tcl, 41 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# bit.tcl --
#
#	Collection of bit manipulation functions.
#
# Copyright (c) 2000, 2001 by Scott Beasley.
#
namespace eval ::bit {
}

proc ::bit::testbit {bit_no flag} {
   return [expr {[expr {$flag & 1 << $bit_no}]?1:0}]
}

proc ::bit::setbit {bit_no in_flag} {
   upvar $in_flag flag
   set flag [expr {1 << $bit_no | $flag}]
}

proc ::bit::clearbit {bit_no in_flag} {
   upvar $in_flag flag
   set flag [expr {$flag ^ 1 << $bit_no}]
}

package provide bit 1.0

set TEST_PACKAGE 0

if {$TEST_PACKAGE} {
   # Test the Bit functions out.
   set flag 127
   set bit_no 7

   puts "flag value is $flag."
   ::bit::setbit $bit_no flag
   puts "bit_no $bit_no is [::bit::testbit $bit_no $flag]"

   puts "flag is now $flag."
   ::bit::clearbit $bit_no flag
   puts "bit_no $bit_no is [::bit::testbit $bit_no $flag]"
   puts "flag is now $flag."
}
Created by Scott Beasley on Sat, 8 Sep 2001 (MIT)
Tcl recipes (162)
Scott Beasley's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks