-2

I've been working on an assignment where I've to use bitwise operators to (OR, AND, or NOT )

the Program has a fixed 4X4 matrix and the user suppose to enter a query to the program ANDing two BINARY numbers, ORing them ...etc

the problem is the "zero leading" binary numbers for example:0111 are shown with value 73 even when I manage to cout it with setfill() and setw() I can't perform the bitwise operation on the actual binary value!

N.B: I've tried strings instead of ints but the bitwise operation still doesn't apply.

For Example:

if I want to AND two binary values let's say int x=1100 and int y=0100 in another int z z=x&y;

the result suppose to be 0100 But the result that appears is 64

which also the result that appears if I tried to print y to the screen

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{

    int Matrix[4][4]={{1,1,0,0},{1,1,0,1},{1,1,0,1},{0,1,0,0}};
    string Doc[4]={"Doc1","Doc2","Doc3","Doc4"};
    string Term[4]={"T1","T2","T3","T4"};

    cout << "THE MATRIX IS:"<<endl;

    for(int i=0;i<4;i++)
    {
        cout<<"\t"<<Doc[i];
    }
    cout<<"\n";
    for(int row=0; row<4;row++)
    {
        cout<<Term[row]<<"\t";
        for(int col=0;col<4;col++)
        {
            cout<<Matrix[row][col]<<"\t";
        }
        cout<<endl;
    }

    int term1=1100;
    cout<<"\nTerm1= "<<term1;
    int term2=1101;
    cout<<"\nTerm2= "<<term2;
    int term3=1101;
    cout<<"\nTerm3= "<<term3;
    int term4=0100;
    cout<<"\nTerm4= "<<setfill('0')<<setw(4)<<term4;


    int Q=term1&term4;
    cout<<"\n Term1 and Term4 ="<<Q;

    system("pause");
    return 0;
}
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Rawan.S
  • 29
  • 1
  • 5
  • Please post code. Also, numeric literals that start with `0` are `octal` in C++, not binary. So `73` is the decimal equivalent of octal `111`. – PaulMcKenzie Dec 03 '15 at 19:21
  • The manipulator `std::dec` should prevent octal interpretation. –  Dec 03 '15 at 19:21
  • 2
    Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – R Sahu Dec 03 '15 at 19:23
  • It seems `0111` gets parsed as octal number. Make sure the stream is set to use a known base. You'll read an integer with that base and decide the bunary value from there (I don't think streams support direct input of binary values). Alternatively use `std::bitset<4>`. – Dietmar Kühl Dec 03 '15 at 19:25
  • `int term1=1100;` -- This sets `term1` to `1100` (one thousand, one hundred). – Keith Thompson Dec 03 '15 at 21:14
  • @keith thompson I know but yet the ANDING of the terms comes correctly except for those with leading zero – Rawan.S Dec 03 '15 at 21:22
  • @Rawan.S: Only by coincidence. `1100 & 1101 == 1100`, but `1100 & 1111 == 1092`. (All the numbers are decimal.) – Keith Thompson Dec 03 '15 at 21:39
  • @KeithThompson Thank you so much at least now I'm not confused anymore :) – Rawan.S Dec 03 '15 at 21:44

2 Answers2

2

When you write 0111 in your code the compiler will assume it's octal since octal numbers start with zero. If you wrote 111 it would be decimal.

C++14 added binary literal prefix so you can write 0b111 to get what you want.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
1

Your question still not clear. You have said you have 4x4 matrix, what type of matrix or 2D array is it? So maybe you can elaborate more.

Regarding dealing with binaries, what students usually confuse about, is that if you are using integer variables, you can use bitwise manipulation over these variables and the result will still be read as an integer format. And if you happen to seek seeing what is happening during the bitwise manipulation and visualize the process, you can always use bitset object as follow.

#include <iostream>
#include <bitset>

int main() {
    int a = 7, b = a>>3, c = a<<2;

    std::cout << "a = " << std::bitset<8>(a)  << std::endl;
    std::cout << "b = " << std::bitset<8>(b)  << std::endl;
    std::cout << "c = " << std::bitset<8>(c) << std::endl;
}

Which should print

00000111
00000000
00011100

So play around with your variables and then visualize them as binaries using bitset is the best way to teach you how HEX, OCT, DEC, and BIN representation works.

And by the way if you are reading 73 as an integer, then this memory address stores 0100 1001 as binary if it's unsigned, and 111 as Octal which is base 8 number representation. See http://coderstoolbox.net/number/

Best of luck

aelbuni
  • 311
  • 1
  • 5