欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C++二進(jìn)制翻轉(zhuǎn)實(shí)例分析

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

本文實(shí)例講述了C++二進(jìn)制翻轉(zhuǎn)的方法,將常用的幾種解決方法羅列出來供大家比較選擇。具體如下:

首先來看看一個(gè)相對(duì)笨拙的算法:

#include <iostream>

using namespace std;

void printBinary(unsigned char str, int size = 1)
{
 int flag = 0x01;
 for (int i = 0; i < size; i++)
 {
 for (int i = 0; i < 8; i++)
 {
  if (str & (0x01 << (7 - i)))
  cout << "1";
  else
  cout << "0";
 }
 cout << endl;;
 }
}

unsigned char mySwap(unsigned char data)
{
 unsigned char flag = 0x01;
 for (int i = 0, j = 7; i < j; i++, j--)
 {
 int right = data & (0x01 << i);
 int left = data & (0x01 << j);
 data &= ~(0x01 << j);
 data &= ~(0x01 << i);
 int dist = j - i;
 data |= (right << dist);
 data |= (left >> dist);
 }
 return data;
}

void main(void)
{
 char source=0x07;
 int i;
 printBinary(source, 1);
 unsigned char result = mySwap(source);
 printBinary(result);
}

下面這個(gè)翻轉(zhuǎn)程序相對(duì)上面實(shí)例而言簡(jiǎn)潔高效:

unsigned char swapBinary(unsigned char data)
{
 int sign = 1;
 unsigned char result = 0;
 for (int i = 0; i <= 7; i++)
 {
 result += ((data & (sign << i)) >> i) << (7 - i);
 }

 return result;
}

下面這個(gè)反轉(zhuǎn)程序比較容易理解:

unsigned char swapBinary2(unsigned char data)
{
 data=(( data & 0xf0) >> 4) | ((data & 0x0f) << 4); 
 data=((data & 0xCC) >> 2) | ((data & 0x33) << 2); 
 data=((data & 0xAA) >> 1) | ((data & 0x55) << 1); 
 return data; 
}

最后這個(gè)超牛的反轉(zhuǎn)程序簡(jiǎn)直碉堡了。。。

unsigned char codeTable[16]={0x00, 0x08, 0x04, 0x0c, 0x02, 0x0a, 0x06, 0x0e, 0x01, 0x09, 0x05, 0x0d, 0x03, 0x0b, 0x07, 0x0f}; 

unsigned char swapBinary3(unsigned char data) 
{ 
 return ((codeTable[data >> 4]) | (codeTable[data & 0x0f] << 4));
}

希望本文所述對(duì)大家C++程序算法設(shè)計(jì)的學(xué)習(xí)有所幫助。

上一篇:C語言實(shí)現(xiàn)顛倒棧的方法

欄    目:C語言

下一篇:C++設(shè)計(jì)模式之建造者模式

本文標(biāo)題:C++二進(jìn)制翻轉(zhuǎn)實(shí)例分析

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3342.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有