BitOperation.cs

2022-05-07 18:29:18 浏览数 (2)

代码语言:javascript复制
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;
        }
    }
}
cs

0 人点赞