zoukankan      html  css  js  c++  java
  • 位数组

    看了《C和指针》后,参考网上的代码自己实现的版本(真的不是纯抄的)。这是5.9 编程练习的第4题。

    头文件bits.h:

    /* bits.h */
    #ifndef BITS_H_INCLUDED
    #define BITS_H_INCLUDED
    
    void set_bit(char bit_array[], unsigned int bit_number);
    
    void clear_bit(char bit_array[], unsigned int bit_number);
    
    void reverse_bit(char bit_array[], unsigned int bit_number);
    
    void assign_bit(char bit_array[], unsigned int bit_number, int value);
    
    int test_bit(char bit_array[], unsigned int bit_number);
    
    #endif // BITS_H_INCLUDED
    

    源代码bits.c

    /* bits.c */
    #include "bits.h"
    
    #define BASE 8
    #define SHIFT 3
    #define MASK 7
    
    void set_bit(char bit_array[], unsigned int bit_number)
    {
        bit_array[bit_number >> SHIFT] |= 1 << (bit_number & MASK);
    }
    
    void clear_bit(char bit_array[], unsigned int bit_number)
    {
        bit_array[bit_number >> SHIFT] &= ~(1 << (bit_number & MASK));
    }
    
    void reverse_bit(char bit_array[], unsigned int bit_number)
    {
        bit_array[bit_number >> SHIFT] ^= 1 << (bit_number & MASK);
    }
    
    void assign_bit(char bit_array[], unsigned int bit_number, int value)
    {
        if (value == 1)
        {
            set_bit(bit_array, bit_number);
        }
        else if (value == 0)
        {
            clear_bit(bit_array, bit_number);
        }
    }
    
    int test_bit(char bit_array[], unsigned int bit_number)
    {
        return (bit_array[bit_number >> SHIFT] & (1 << (bit_number & MASK)))
            >> (bit_number & MASK);
    }
    

     测试用例:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include "bits.h"
    
    int main()
    {
        char bits[128];
        memset(bits, 0, sizeof(bits));
        set_bit(bits, 234);
        printf("%d\n", test_bit(bits, 234));
        clear_bit(bits, 234);
        printf("%d\n", test_bit(bits, 234));
        reverse_bit(bits, 235);
        printf("%d\n", test_bit(bits, 235));
        reverse_bit(bits, 235);
        printf("%d\n", test_bit(bits, 235));
        assign_bit(bits, 1024, 1);
        printf("%d\n", test_bit(bits, 1024));
    
        return 0;
    }
    

    答案版本:

    头文件bitarray.h

    #ifndef BITARRAY_H_INCLUDED
    #define BITARRAY_H_INCLUDED
    
    /*
     * Prototypes for a suite of functions that implement an array of bits in a
     * character array.
     */
    
    // Set a specific bit
    void set_bit(char bit_array[], unsigned int bit_number);
    
    // Clear a specific bit
    void clear_bit(char bit_array[], unsigned int bit_number);
    
    // Assign a value to a bit
    void assign_bit(char bit_array[], unsigned int bit_number, int value);
    
    // Test a specific bit
    int test_bit(char bit_array[], unsigned int bit_number);
    
    #endif // BITARRAY_H_INCLUDED
    

    源代码bitarray.c

    /* bitarray.cpp -- Implements an array of bits in a character array. */
    
    #include <limits.h>
    
    #include "bitarray.h"
    
    static unsigned int character_offset(unsigned int bit_number);
    static unsigned int bit_offset(unsigned int bit_number);
    
    void set_bit(char bit_array[], unsigned int bit_number)
    {
        bit_array[character_offset(bit_number)] |=
            1 << bit_offset(bit_number);
    }
    
    void clear_bit(char bit_array[], unsigned int bit_number)
    {
        bit_array[ character_offset( bit_number ) ] &=
            ~(1 << bit_offset(bit_number));
    }
    
    void assign_bit(char bit_array[], unsigned int bit_number, int value)
    {
        if (value != 0)
            set_bit(bit_array, bit_number);
        else
            clear_bit(bit_array, bit_number);
    }
    
    int test_bit(char bit_array[], unsigned int bit_number)
    {
        return (bit_array[ character_offset( bit_number ) ] &
            1 << bit_offset( bit_number )) != 0;
    }
    
    static unsigned int character_offset(unsigned int bit_number)
    {
        return bit_number / CHAR_BIT;
    }
    
    static unsigned int bit_offset(unsigned int bit_number)
    {
        return bit_number % CHAR_BIT;
    }
    
  • 相关阅读:
    更新主窗口控件的内容1:子线程工作时同时更新主线程内的控件内容
    静态类和非静态类中静态变量
    js $的扩展写法
    js 获取时区
    export to excel
    使用Sqlserver事务发布实现数据同步(zhuanqian)
    json to entity in api
    automapper demo
    autoMapper的介绍
    Springboot的热部署
  • 原文地址:https://www.cnblogs.com/qwertWZ/p/2941365.html
Copyright © 2011-2022 走看看