• BitOperation.cs


    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Bit
    {
        /*class methods for bits operation*/
        public class BitOperation
        {
            public Byte setBit(Byte targetByte, int targetPos, int value)
            {
                if ((value == 0 || value == 1) && (targetPos >= 0 && targetPos < 8))
                {
                    int targetPosValue = -1;
                    switch (targetPos)
                    {
                        case 0:
                            targetPosValue = (targetByte >> 7) & 0x01;
                            if (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                            {
                                break;
                            }
                            else
                            {
                                if (value == 1)
                                {
                                    targetByte = Convert.ToByte(targetByte | 0x80);
                                }
                                else if (value == 0)
                                {
                                    targetByte = Convert.ToByte(targetByte & 0x7f);
                                }
                                break;
                            }
                        case 1:
                            targetPosValue = (targetByte >> 6) & 0x01;
                            if (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                            {
                                break;
                            }
                            else
                            {
                                if (value == 1)
                                {
                                    targetByte = Convert.ToByte(targetByte | 0x40);
                                }
                                else if (value == 0)
                                {
                                    targetByte = Convert.ToByte(targetByte & 0xbf);
                                }
                                break;
                            }
                        case 2:
                            targetPosValue = (targetByte >> 5) & 0x01;
                            if (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                            {
                                break;
                            }
                            else
                            {
                                if (value == 1)
                                {
                                    targetByte = Convert.ToByte(targetByte | 0x20);
                                }
                                else if (value == 0)
                                {
                                    targetByte = Convert.ToByte(targetByte & 0xdf);
                                }
                                break;
                            }
                        case 3:
                            targetPosValue = (targetByte >> 4) & 0x01;
                            if (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                            {
                                break;
                            }
                            else
                            {
                                if (value == 1)
                                {
                                    targetByte = Convert.ToByte(targetByte | 0x10);
                                }
                                else if (value == 0)
                                {
                                    targetByte = Convert.ToByte(targetByte & 0xef);
                                }
                                break;
                            }
                        case 4:
                            targetPosValue = (targetByte >> 3) & 0x01;
                            if (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                            {
                                break;
                            }
                            else
                            {
                                if (value == 1)
                                {
                                    targetByte = Convert.ToByte(targetByte | 0x08);
                                }
                                else if (value == 0)
                                {
                                    targetByte = Convert.ToByte(targetByte & 0xf7);
                                }
                                break;
                            }
                        case 5:
                            targetPosValue = (targetByte >> 2) & 0x01;
                            if (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                            {
                                break;
                            }
                            else
                            {
                                if (value == 1)
                                {
                                    targetByte = Convert.ToByte(targetByte | 0x04);
                                }
                                else if (value == 0)
                                {
                                    targetByte = Convert.ToByte(targetByte & 0xfb);
                                }
                                break;
                            }
                        case 6:
                            targetPosValue = (targetByte >> 1) & 0x01;
                            if (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                            {
                                break;
                            }
                            else
                            {
                                if (value == 1)
                                {
                                    targetByte = Convert.ToByte(targetByte | 0x02);
                                }
                                else if (value == 0)
                                {
                                    targetByte = Convert.ToByte(targetByte & 0xfd);
                                }
                                break;
                            }
                        case 7:
                            targetPosValue = targetByte & 0x01;
                            if (targetPosValue == value)   //如果指定位的值和传的参数值相同,则没有任何操作,返回
                            {
                                break;
                            }
                            else
                            {
                                if (value == 1)
                                {
                                    targetByte = Convert.ToByte(targetByte | 0x01);
                                }
                                else if (value == 0)
                                {
                                    targetByte = Convert.ToByte(targetByte & 0xfe);
                                }
                                break;
                            }
                        default:
                            break;
                    }
                    return targetByte;
                }
                else
                {
                    return 0;
                }
            }
    
            public int getBit(Byte targetByte, int targetPos)
            {
                int value = -1;
    
                switch (targetPos)
                {
                    case 0:
                        value = (targetByte >> 7) & 0x01;
                        break;
                    case 1:
                        value = (targetByte >> 6) & 0x01;
                        break;
                    case 2:
                        value = (targetByte >> 5) & 0x01;
                        break;
                    case 3:
                        value = (targetByte >> 4) & 0x01;
                        break;
                    case 4:
                        value = (targetByte >> 3) & 0x01;
                        break;
                    case 5:
                        value = (targetByte >> 2) & 0x01;
                        break;
                    case 6:
                        value = (targetByte >> 1) & 0x01;
                        break;
                    case 7:
                        value = targetByte & 0x01;
                        break;
                    default:
                        break;
                }
                return value;
            }
        }
    }
    
  • 相关阅读:
    2022的pycharm创建项目的时候可以直接选择FastAPI了~
    玩转服务器博客两件套之绝佳的Markdown写作平台CodiMD
    使用 Spring Data JPA 按日期和时间查询实体
    Kafka 介绍
    页面置换算法
    与设备无关的I/O软件
    中断机构和中断处理程序
    磁盘存储器的性能和调度
    设备驱动程序
    缓冲区管理
  • 原文地址:https://www.cnblogs.com/jingmoxukong/p/2121294.html
Copyright © 2020-2023  润新知