Bitwise AND

Hi

I have the following macro which is intended to bitwise AND the string of binary numbers in the two text entries, fTe1 and fTe2, and put the result in another text entry, fTe3. Unfortunately I get “out of bounds error”. I have commented the instructions that I used to AND the two strings.

#include <TGClient.h>
#include <TGFrame.h>
#include <TGTextEntry.h>
#include <TGButton.h>
#include <TString.h>
#include <RQ_OBJECT.h>

class MainFrame
{
	RQ_OBJECT("MainFrame")
	private:
		TGMainFrame *fMain;
		TGCompositeFrame *f1, *f2;
		TGLayoutHints *fL1, *fL2, *fL3;
		TGTextEntry *fTe1;
		TGTextEntry *fTe2;
		TGTextEntry *fTe3;
		TGButton *ANDButton;
	
	public:
		MainFrame(const TGWindow *p,UInt_t w,UInt_t h);
		virtual ~MainFrame();
		void AND();
};

MainFrame::MainFrame(const TGWindow *p,UInt_t w,UInt_t h)
{
	fMain = new TGMainFrame(p, w, h);

	fMain->ChangeOptions((fMain->GetOptions() & ~kVerticalFrame) | kHorizontalFrame);

	f1 = new TGCompositeFrame(fMain, 60, 20, kVerticalFrame);
	f2 = new TGCompositeFrame(fMain, 100, 20, kVerticalFrame | kFixedWidth);

	fL1 = new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX, 10, 10, 10, 2);
	fL2 = new TGLayoutHints(kLHintsTop | kLHintsRight, 2, 5, 10, 0);
	fL3 = new TGLayoutHints(kLHintsTop, 5, 5, 60, 5);

	fTe1 = new TGTextEntry(f1, new TGTextBuffer(5),1);
	f1->AddFrame(fTe1, new TGLayoutHints(kLHintsTop | kLHintsExpandX, 5, 5, 5, 5));
        fTe1->Insert("1111001011");

	fTe2 = new TGTextEntry(f1, new TGTextBuffer(5),2);
	f1->AddFrame(fTe2, new TGLayoutHints(kLHintsTop | kLHintsExpandX, 5, 5, 5, 5));
        fTe2->Insert("1010110101");

	fTe3 = new TGTextEntry(f1, new TGTextBuffer(5),3);
	f1->AddFrame(fTe3, new TGLayoutHints(kLHintsTop | kLHintsExpandX, 5, 5, 15, 5));
   	
	//AND button on frame f2
	ANDButton = new TGTextButton(f2, "&AND", 4);
	//ANDButton->Connect("Clicked()","MainFrame",this,"AND()");
	f2->AddFrame(ANDButton,fL3);

	fMain->AddFrame(f1,fL1);
	fMain->AddFrame(f2,fL2);
	
	fMain->SetWindowName("Bitwise ANDing");
	fMain->MapSubwindows();
	fMain->Resize(400, 200);
  	fMain->MapWindow(); 
}

MainFrame::~MainFrame()
{
	fMain->Cleanup();
	delete fMain;
}

/*void MainFrame::AND()
{
	TString str1 = fTe1->GetText();
	TString str2 = fTe2->GetText();
	TString str3;
	for(int i=0;i<10;i++)
	{
		if((str1[i]=="1") && (str2[i]=="1"))
			str3[i] = "1";
		else 
			str3[i] = "0";
	}
	fTe3->SetText(str3.Data());
}*/

void BitwiseAND()
{
   	new MainFrame(gClient->GetRoot(), 300, 200);
}

Any suggestions of where I may be going wrong?

Cheers

Hi,

Try like this:

for (int i=0;i<10;i++) { if((str1[i] == '1') && (str2[i] == '1')) str3 += '1'; else str3 += '0'; }
And note that you should check for the validity of the indexes, to avoid going out of bound
Cheers, Bertrand.

Thank you bellenot.

Note that all of this assumes that the string it 10 digits long. In the long run, you may be better off:

  1. converting binary string to integer (where you check the size of the of the strings)
  2. anding integers
  3. converting integer to binary string

Cheers,
Charles

Hi,

So does this mean that the assumption that the string is 10 digits long is because of my index going up to 10, or is it because the string only supports up to 10 digits long in order for the code to work properly whereupon if it is longer than that we have to perform your suggestions?

Cheers

[quote=“manaila”]So does this mean that the assumption that the string is 10 digits long is because of my index going up to 10, or is it because the string only supports up to 10 digits long in order for the code to work properly whereupon if it is longer than that we have to perform your suggestions?
[/quote]

Strings can support virtually any length. Your code won’t do the right thing if the string is less than 10 characters long, if the string is more than 10 characters long, or if the strings are of different lengths. For this example, you are 'and’ing, so you want to use the minimum of the two lengths. If you are 'or’ing, you have to be more careful. As I said, you are probably better off converting each string individually into an integer and then doing whatever calculation and output is necessary.

Cheers,
Charles

Thank you very much.