-3

I have a question. I have a data set of 32 by 32 integer matrix. As int holds 32 bit, i want to break down each elements of my matrix into 32 bit, suppose i have 255 decimal at first place of matrix and this can hold 32 bit as of integer, now i want to convert this 255 decimal into binary 255 i.e. 11111111 and padding with zero the remaining positions. I don't want to construct another matrix because then the data size will be greater than 32 bits. I want to do this with C++. Something like this...

a[32][32];
for(int i=0;i<32;i++)
for(int j=0;j<32;j++)
a[i][j]=255+i+j;

Now let a[0][0]=255 in decimal form...i want to convert this and each element of matrix a into decimal and which will be like this..a[0][0]=00000000000000000000000011111111. This is 32 bit as int can hold 32 bit. Now my question is how i can access each bit of the a[0][0]. The important thing is that i want to stay in 32 bit format and don't want to create another matrix.

If you have any solution then please share with me. I am new to programming world. Thanks

  • 2
    The `int`s in your matrix are already stored in binary. It may help your question to describe what you are trying to achieve that you think you can't do with your existing matrix. – Drew Dormann Apr 24 '13 at 05:12
  • everything is stored as binary. maybe you can mask the rest of the MS bytes.. – Koushik Shetty Apr 24 '13 at 05:14
  • Thanks for the reply. I have two different matrix of 32*32 size. i want to convert each element of both matrix into 32 bit and then i want to perform bit wise XOR of respective elements and some other operations. – hamid Surnamelukhnowi Apr 24 '13 at 05:19
  • (Big) If I understand you correctly, you want to print the integers in binary form. See: http://stackoverflow.com/questions/7349689/c-how-to-print-using-cout-the-way-a-number-is-stored-in-memory – Tony Delroy Apr 24 '13 at 05:22
  • @hamidSurnamelukhnowi Is that what you're asking? How to *print* the individual bits? – Drew Dormann Apr 24 '13 at 05:23
  • 1
    "i want to convert each element of both matrix into 32 bit and then i want to perform bit wise XOR of respective elements and some other operations." - you can do bitwise operations on all bits in an `int` simultaneously... e.g. `int x = 1, y = 3; y ^= x;`. `^` is xor, `|` is or, `&` is and, `<< n` is shift left `n` bits, `>>` right etc.. – Tony Delroy Apr 24 '13 at 05:24
  • Drew and Tony Sir, thanks a lot for the reply. You got it right but after Xoring i have some other operations also to do with them that is bit wise...I hope you have heard about DES algorithms..I want to convert each element into 32 bit and then take 2 integers(64 bit) and perform later operations....Later operations include XOR, permutation and S-Box substitution..these all are bit wise oeprations. Where every bit has its own functions to be performed. I don't want to print, i want to save a binary 32 bits in int. Thanks – hamid Surnamelukhnowi Apr 24 '13 at 05:32
  • 3
    @hamidSurnamelukhnowi This question makes no sense at all. Let's start at the beginning, edit your question and show us **in code** what your 32x32 matrix looks like. I'm guessing it is `int m[32][32];` but please confirm. Then show use how you get '255 decimal at first place of matrix'. I'm also guessing that is `m[0][0] = 255;` but again please confirm. It much easier to write code than it is to describe code, please show us some of the code you already have. – john Apr 24 '13 at 05:35
  • 3
    If you have a 32 by 32 matrix of 32bit integers, they are already in integer/binary format and you can modify them using bitwise operations directly. If you need to treat them as bytes or as bits, use bitshifts and masking. – Patashu Apr 24 '13 at 05:42
  • @John Sir..i edited the questions and i hope now this will clear my points. Thanks – hamid Surnamelukhnowi Apr 25 '13 at 01:04
  • @hamidSurnamelukhnowi The numbers are **already in binary**. There is nothing to convert. Converting them to decimal numbers which look like binary numbers is possible but it's counter-productive to what you really want to do. You can do bitwise operations on them already using `&`, `|`, `^`, `<<`, `>>`, and `~`; see: https://en.wikipedia.org/wiki/Bitwise_operations_in_C – Boann Apr 25 '13 at 01:39
  • @Boann i understood this but now my question is how i can access each bit of this binary...as you said they are saved as binary so now how i can access each bit of this binary..thanks – hamid Surnamelukhnowi Apr 25 '13 at 03:24
  • @hamidSurnamelukhnowi I've written a hopefully useful answer below. – john Apr 25 '13 at 06:56

1 Answers1

1

To access bits of an integer you must use the bitwise operators.

To test if the nth bit is set use & and <<

if (m[0][0] & (1 << n))
    cout << "bit " << n << " is set";

Remember to count from 0, so to test if the first (or lowest) bit is set use 1 << 0, the second use 1 << 1 etc.

To set the nth bit use |= and <<

m[0][0] |= (1 << n);

To clear the nth bit use &=, ~ and <<

m[0][0] &= ~(1 << n);

To invert the nth bit use ^= and <<

m[0][0] ^= (1 << n);

There are lots of other possibilities including testing or setting multiple bits at a time, extracting bit fields etc. I suggest you read a tutorial on this. There are lots and lots of these on the internet, for instance this one.

john
  • 85,011
  • 4
  • 57
  • 81