الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

[ تمت الإجابة ]مساعدة يا أخوان في عمليات Bitwise And / Bitwise Or و نظام العد الثنائي,

بدأه Wolf_Oman في 22 أبريل 2009 · 6 رد · 1,122 مشاهدة · في أرشيف قسم الـــ Assembly
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

أعتذر عن طرح الموضوع مجددا بهذا القسم فلم أجد ردا عليه بقسم السي بلس icon1.gif

لدي إستفسار حول نظام العد الثنائي و كيفية تطبيقة على العمليات البرمجية

قمت بتصميم وظيفة لعملية

bitwise AND

بنسبة للأعداد الموجبة سهلة إستطعت برمجة هذه العمليات على نظام العد الثنائي

فمثلا لو أردنا القيام بعملية

bitwise AND

لرقم 20 و الرقم 50

أولا نحول الرقم 20 لنظام العد الثنائي لتصبح

10100

ثم نحول الرقم 50 لنضام العد الثنائي لتصبح

110010

ثم نقوم بعملية المقارنة بطريقة

bitwise AND

010100

110010

بعد المقارنة

010000

لتكون النتيجة

16

طبعا الوظيفة التي أقوم بتصميمها تقوم بنفس الطريقة السابقة و بدون إستخدام وظائف اللغة المخصصة لمثل هذه العمليات

إستفساري حول الأعداد السالبة أولا كيف نحولها لنظام العد الثنائي ثم كيف يتم مقارنتها بطريقة

bitwise AND

بحثت على الشبكة و كل الطرق تقول الآتي

بأن نتعامل مع الرقم على أنه رقم أو عدد موجب ثم نقوم بتغير الصفر إلى واحد و الواحد إلى صفر ثم

نظيف الرقم المتمم واحد ثم نقارن إلا أن النتائج التي حصلت عليها ليست صحيحة وذلك عند مقرنتها

بنتائج الوظائف المخصصة لهذه العملية بانتظار المساعدة و لكم جزيل الشكر و خالص التحية

في أمان الله

Wolf_Oman

صرح السماء

wolf9228

ناقوس الخطر

#2
Wolf_Oman كتب:
إلا أن النتائج التي حصلت عليها ليست صحيحة وذلك عند مقرنتها

بنتائج الوظائف المخصصة لهذه العملية

ما هي هذه الوظائف التي تتكلم عنها، هات مثال كامل.

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

#3

Xacker

مرحبا أخوي

بنسبة للوظيفة التي أقوم بتصميمها هي على لغة

autoit

توجد بهذه اللغة وظائف خاصة لهذه العملية كتالي


BitAND ( value1, value2 [, value n] )
BitXOR ( value1, value2 [, value n] )
BitNOT ( value )

مثل

Visual FoxPro

أدخل هذا الرابط

http://msdn.microsoft.com/en-us/library/2hxce09y(VS.71).aspx

أما على لغة السي بلس لا توجد وظائف إنما يمكن استخدام هذه العمليات بطريقة مباشرة أو حتى عمل وظيفة

كمثال التالي


{
return x ^ y ^ z;
}
inline unsigned int BitXOR(unsigned int x, unsigned int y, unsigned int z)

أما ما أود القيام به هووه إستخدام العد الثنائي و ليس وظائف اللغات العادية لهذه العملية

طبعا بمساعدة وظائف

String

أو كما على السي بلس 6

mfc

CString

و إذا كان بمقدورك شرح هذه العملية على نظام العد الثنائي أكون شاكرا و ممتنا لك

في أمان الله

تم تعديل هذه المشاركة بواسطة Wolf_Oman في 22 أبريل 2009 في 21:29

Wolf_Oman

صرح السماء

wolf9228

ناقوس الخطر

#4

أنا أتكلم بكل بساطة عن النتائج التي تقارن معها.

انت تحسب شئ بشكل يدوي من خلال استخدام المتمم الثنائي، ومن ثم تقارنه مع نتيجة ما، طيب هذه النتيجة من أين جئت بها؟

بالمناسبة كلامك عن المتمم الثنائي صحيح، لو كنت تريد قيمة -5 بالترميز الثنائي فهي من خلال أخذ قيمتها بشكل ثنائي وهي موجبة (00000101) ومن ثم قلبها باستخدام NOT (11111010) ومن ثم إضافة 1 لها لتصبح (11111011).

تم تعديل هذه المشاركة بواسطة Xacker في 23 أبريل 2009 في 01:09

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

#5
Xacker كتب:
أنا أتكلم بكل بساطة عن النتائج التي تقارن معها.

انت تحسب شئ بشكل يدوي من خلال استخدام المتمم الثنائي، ومن ثم تقارنه مع نتيجة ما، طيب هذه النتيجة من أين جئت بها؟

بالمناسبة كلامك عن المتمم الثنائي صحيح، لو كنت تريد قيمة -5 بالترميز الثنائي فهي من خلال أخذ قيمتها بشكل ثنائي وهي موجبة (00000101) ومن ثم قلبها باستخدام NOT (11111010) ومن ثم إضافة 1 لها لتصبح (11111011).

شكرا أخوي ما قصرت و إذا لم يكن هناك من عناء و مشقة عليك فقد أعدت تصميم الوظيفة على السي بلس باستخدام مكتبة

MFC

تعمل على الأعداد الموجبة و تعطي قيمة صحيحة

لو كان لديك الوقت لإكمالها من حيث تشمل الأعداد السالبة فهذا من كرمك و جميل أخلاقك

و إن لم تجد الوقت لذالك فقد كفيت و وفيت و جزاك الله خيرا و بركة

وهذه هي الوظيفة على السي بلس

// Bit_ANDDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Bit_AND.h"
#include "Bit_ANDDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int Bit_AND(int value1, int value2);
CString Float_To_String(float V_FLOAT);
/////////////////////////////////////////////////////////////////////////////
// CBit_ANDDlg dialog

CBit_ANDDlg::CBit_ANDDlg(CWnd* pParent /*=NULL*/)
 : CDialog(CBit_ANDDlg::IDD, pParent)
{
 //{{AFX_DATA_INIT(CBit_ANDDlg)
  // NOTE: the ClassWizard will add member initialization here
 //}}AFX_DATA_INIT
 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CBit_ANDDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(CBit_ANDDlg)
  // NOTE: the ClassWizard will add DDX and DDV calls here
 //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBit_ANDDlg, CDialog)
 //{{AFX_MSG_MAP(CBit_ANDDlg)
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBit_ANDDlg message handlers

BOOL CBit_ANDDlg::OnInitDialog()
{
 CDialog::OnInitDialog();

 // Set the icon for this dialog.  The framework does this automatically
 //  when the application's main window is not a dialog
 SetIcon(m_hIcon, TRUE);   // Set big icon
 SetIcon(m_hIcon, FALSE);  // Set small icon

 // TODO: Add extra initialization here

 return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CBit_ANDDlg::OnPaint() 
{
 if (IsIconic())
 {
  CPaintDC dc(this); // device context for painting

  SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

  // Center icon in client rectangle
  int cxIcon = GetSystemMetrics(SM_CXICON);
  int cyIcon = GetSystemMetrics(SM_CYICON);
  CRect rect;
  GetClientRect(&rect);
  int x = (rect.Width() - cxIcon + 1) / 2;
  int y = (rect.Height() - cyIcon + 1) / 2;

  // Draw the icon
  dc.DrawIcon(x, y, m_hIcon);
 }
 else
 {
  CDialog::OnPaint();
 }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CBit_ANDDlg::OnQueryDragIcon()
{
 return (HCURSOR) m_hIcon;
}
//[MASK_OPTIONEL] The nth number - up to 255 values can be specified.
int Bit_AND(int value1, int value2)
{
float result = (float) value1;
CString str = Float_To_String(result);
CString Txt1 = "";
CString iResult;
int Fint;
do
{
result = _ttoi(str);
result = (result / 2);
iResult = Float_To_String(result);
if (strstr(iResult,".") != NULL)
{
Fint = iResult.Find(".",0);
str = iResult.Left(Fint);
Txt1.Insert(0,"1");
} else {
str = iResult;
Txt1.Insert(0,"0");
}
} while (result >= 1);


result = (float) value2;
str = Float_To_String(result);
CString Txt2 = "";
iResult = "";
do
{
result = _ttoi(str);
result = (result / 2);
iResult = Float_To_String(result);
if (strstr(iResult,".") != NULL)
{
Fint = iResult.Find(".",0);
str = iResult.Left(Fint);
Txt2.Insert(0,"1");
} else {
str = iResult;
Txt2.Insert(0,"0");
}
} while (result >= 1);




int IY;
if  (strlen(Txt1) < strlen(Txt2)) 
{
IY = strlen(Txt2) - strlen(Txt1);
for (int M = 0; M < IY; M++)
Txt1.Insert(0,"0");
}
if  (strlen(Txt2) < strlen(Txt1))
{
IY = strlen(Txt1) - strlen(Txt2);
for (int M = 0; M < IY; M++)
Txt2.Insert(0,"0");
}





CString Alloperation_result = "";
CString operation_result = "";
CString operation = "";
for (int M = 1; M <= Txt1.GetLength(); M++)
{
operation = Txt1.Left(M).Right(1) + Txt2.Left(M).Right(1);
if (operation == "11") operation_result = "1";
if (operation == "00") operation_result = "0";
if (operation == "10") operation_result = "0";
if (operation == "01") operation_result = "0";
Alloperation_result += operation_result;

}








int twos = 1, ones;

for (M = 1; M <= Alloperation_result.GetLength(); M++)
{

if (M > 2 ) twos *= 2;
if (M == 1) twos = 1;
if (M == 2) twos = 2;
if (Alloperation_result.Right(M).Left(1) == (CString) "1" ) ones += (twos);
}
return ones;
}


CString Float_To_String(float V_FLOAT)
{
CString iResult;
iResult.Format(_T("%g"), V_FLOAT);
return iResult;
}

void CBit_ANDDlg::OnButton1() 
{
int ones = Bit_AND(10000, 500);
CString StrResult = "";
StrResult.Format(_T("%d"),  ones);
::AfxMessageBox(StrResult );
}

وهذه أيضا الوظيفة على لغة

AUTOIT

#Include <Array.au3>

Bit_AND(50, 20, "" )

Func Bit_AND($value1, $value2 , $value )
$BOOL =  IsNumber($value) Or StringStripWS($value, 8) = "" 
if IsNumber($value1) And IsNumber($value2) And ($BOOL) Then
$value1 = StringStripWS($value1, 8)
$value2 = StringStripWS($value2, 8)
$value = StringStripWS($value, 8)
$Ivalue1 = StringReplace($value1,"-","")
$Ivalue2 = StringReplace($value2,"-","")
$Ivalue = StringReplace($value,"-","")

$result = $Ivalue1 
$Txt1 = ""
Select
Case StringInStr($value1,"-") = False
Do
$result = StringSplit($result,".")
$result = $result[1]
$result = ($result / 2)
Select
Case StringInStr($result,".") = True
$Txt1 = "1" & $Txt1
Case StringInStr($result,".") = False
$Txt1 = "0" & $Txt1
EndSelect
Until($result < 1)


Case StringInStr($value1,"-") = True
Do
$result = StringSplit($result,".")
$result = $result[1]
$result = ($result / 2)
Select
Case StringInStr($result,".") = True
$Txt1 = "0" & $Txt1
Case StringInStr($result,".") = False
$Txt1 = "1" & $Txt1
EndSelect
Until($result < 1)
$Txt1 &= "1" 
EndSelect




$result = $Ivalue2 
$Txt2 = ""
Select
Case StringInStr($value2,"-") = False
Do
$result = StringSplit($result,".")
$result = $result[1]
$result = ($result / 2)
Select
Case StringInStr($result,".") = True
$Txt2 = "1" & $Txt2
Case StringInStr($result,".") = False
$Txt2 = "0" & $Txt2
EndSelect
Until($result < 1)
Case StringInStr($value2,"-") = True
Do
$result = StringSplit($result,".")
$result = $result[1]
$result = ($result / 2)
Select
Case StringInStr($result,".") = True
$Txt2 = "0" & $Txt2
Case StringInStr($result,".") = False
$Txt2 = "1" & $Txt2
EndSelect
Until($result < 1)
$Txt2 = "1" & $Txt2
EndSelect




Select
Case StringLen($Txt1) < StringLen($Txt2) 
$IY = StringLen($Txt2) - StringLen($Txt1)
For $IX = 1 To $IY Step 1
if StringInStr($value1,"-") = False Then
$Txt1 = "0" & $Txt1
Else
$Txt1 = "1" & $Txt1
EndIf
Next
Case StringLen($Txt1) > StringLen($Txt2) 
$IY = StringLen($Txt1) - StringLen($Txt2)
For $IX = 1 To $IY Step 1
if StringInStr($value2,"-") = False Then
$Txt2 = "0" & $Txt2
Else
$Txt2 = "1" & $Txt2
EndIf
Next
EndSelect


$ArrayTxt1 = StringSplit($Txt1,"") 
$ArrayTxt2 = StringSplit($Txt2,"") 
$Alloperation_result = ""
$operation_result = ""
For $i = 1 To $ArrayTxt1[0] Step 1
$operation = String($ArrayTxt1[$i] & $ArrayTxt2[$i])

Select
Case $operation = "11";True && True = True
$operation_result = "1";True
Case $operation = "00";False && False = False
$operation_result = "0";False
Case $operation = "10";True && False = False
$operation_result = "0";False
Case $operation = "01";False && False = False
$operation_result = "0";False
EndSelect
$Alloperation_result &= $operation_result
Next



If $Ivalue <> "" Then
$result = $Ivalue 
$Txt3 = ""
Select
Case StringInStr($value,"-") = False
Do
$result = StringSplit($result,".")
$result = $result[1]
$result = ($result / 2)
Select
Case StringInStr($result,".") = True
$Txt3 = "1" & $Txt3
Case StringInStr($result,".") = False
$Txt3 = "0" & $Txt3
EndSelect
Until($result < 1)

Case StringInStr($value,"-") = True
Do
$result = StringSplit($result,".")
$result = $result[1]
$result = ($result / 2)
Select
Case StringInStr($result,".") = True
$Txt3 = "0" & $Txt3
Case StringInStr($result,".") = False
$Txt3 = "1" & $Txt3
EndSelect
Until($result < 1)
$Txt3 = "1" & $Txt3
EndSelect

Select
Case StringLen($Txt2) < StringLen($Txt3) 
$IY = StringLen($Txt3) - StringLen($Txt2)
For $IX = 1 To $IY Step 1
$Txt2 = "0" & $Txt2
Next
Case StringLen($Txt2) > StringLen($Txt3) 
$IY = StringLen($Txt2) - StringLen($Txt3)
For $IX = 1 To $IY Step 1
$Txt3 = "0" & $Txt3
Next
EndSelect

$ArrayTxt2 = StringSplit($Txt2,"") 
$ArrayTxt3 = StringSplit($Txt3,"") 
$Alloperation_result = ""
$operation_result = ""
For $i = 1 To $ArrayTxt2[0] Step 1
$operation = String($ArrayTxt2[$i] & $ArrayTxt3[$i])
Select
Case $operation = "11";True && True = True
$operation_result = "1";True
Case $operation = "00";False && False = False
$operation_result = "0";False
Case $operation = "10";True && False = False
$operation_result = "0";False
Case $operation = "01";False && False = False
$operation_result = "0";False
EndSelect
$Alloperation_result &= $operation_result
Next
EndIf
$twos = 1;Start from 1
$ones = 0 

For $I = 1 To StringLen($Alloperation_result) Step 1
IF $I > 2 Then $twos *= 2
IF $I = 1 Then $twos = 1
IF $I = 2 Then  $twos = 2
$String = StringLeft(StringRight($Alloperation_result,$I),1)
IF $String = "1" Then $ones += ($twos)
Next

MsgBox(0,"",$ones)


Else
Return -1
EndIf
EndFunc

في أمان الله

Wolf_Oman

صرح السماء

wolf9228

ناقوس الخطر

#6

طالما الموضوع خرج عن الأسمبلي فعليك وضع المشاركة الأخيرة في قسم ++C

تقبل تحياتي

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

#7
Xacker كتب:
طالما الموضوع خرج عن الأسمبلي فعليك وضع المشاركة الأخيرة في قسم ++C

تقبل تحياتي

مشكور أخوي ما قصرت عرضت الموضوع مسبقا في قسم السي بلس و في عضو ما قصر ساعدني بس حبيت أوضحلك كلامي

و إن شاء الله لو إحتجت مساعدة أنتوا ما تقصرو و جزاكم الله خير على هذي المجهودات المبذولة في هذا الموقع الرائع في أمان الله

Wolf_Oman

صرح السماء

wolf9228

ناقوس الخطر

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…