Solved

C++ Bitwise

Posted on 2014-04-11
2
301 Views
Last Modified: 2014-04-12
I have an array of Char and I am trying to output the bits. I am a little confused on the concepts of mask and shift (looking for a good article to understand these?)

Not sure if either option 1 or option 2 are correct

  for (int x = 0; i < a.arraySize; x++)
   {
         int test1 = sizeof( char );
   const int SHIFT = 8 * sizeof( char ) - 1;
   const unsigned MASK = 1 << SHIFT;

   os << setw( 10 ) <<  " start =  " << a.barray[x] << " = ";
   char test = a.barray[x];
    unsigned test2 = a.barray[x];
         // display bits
         for ( unsigned j = 1; j <= SHIFT + 1; j++ )
         {
               // Option 1
              //  os << ( a.barray[j] & MASK ? '1' : '0' );
            //  a.barray[j] <<= 1; // shift value left by 1
                Option 2
              os << ( test2 & MASK ? '1' : '0' );
              test2 <<= 1; // shift value left by 1
              if ( j % 8 == 0 ) // output a space after 8 bits
                   os << ' ';
         } // end for
   } // end for
0
Comment
Question by:pcomb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 39993927
Hi pcomb,

You seem to have the right idea.  :)  A small suggestion though is to leave the data "as is" and shift the mask.

Assume that the char variable contains an 'A'.  The numeric value of the 'A' is 65 decimal, 41 hex, 101 octal, and 100001 binary.  The looping that you're writing should display the binary value, showing the bits from left to right.

The inner loop should probably have look similar to this:

unsigned char Value = 'A';
unsigned char Mask;

for  (Mask = 0x80; Mask; Mask >>= 1)
  os << (Value & Mask) ? '1' : '0';

Open in new window


You can wrap this tight loop with the controls to process the entire array, too.


Good Luck,
Kent
0
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 500 total points
ID: 39994062
I am a little confused on the concepts of mask and shift (looking for a good article to understand these?)
Have you tried Wikipedia?

http://en.wikipedia.org/wiki/Mask_(computing)
http://en.wikipedia.org/wiki/Bitwise_operation

Bit masking and shifting should be easy to understand IF you first understand the idea of big endian and little endian.  But that's only for how multiple bytes are joined to for integer values greater than 255.

In the case of a char, you're talking about only 8 bits, and they are always logically arranged like this:
00000000 = Zero
00000001 = One
00000010 = Two
00000100 = Four
00001000 = Eight
00010000 = Sixteen
00100000 = Thirty-two
01000000 = Sixty-four
10000000 = One-hundred-twenty-eight

You simply add those together for any other value between 0 and 255.  So as a simple example, Five is 00000100 + 00000001.

But shifting is simply moving the digits to the left or right.  So if you start with say Two and shift to the left (<<) two places you start with
00000010 <<  and you wind up with
00001000 because the 1's are shifted to the left.

Bit Masking is how you determine if a particular bit is turned on or off by taking the value in question and doing a logical AND against a mask.  So if you want to know if a particular value has the 7th bit (i.e. the bit representing a value of Two) turned on, you simply AND the value in question with 00000010.

So the value for Sixty-six is 01000010.  Is the 7th bit turned on?
01000010 - value in question
00000010 - Mask for the 7th bit
======== - AND the bits together
00000010
Since the result is equal to the Mask, then the 7th bit is turned on.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
zeroMAx challenge 20 134
bunnyEars2 challenge 6 181
Interview question Javascript, database 12 111
Python 3.5.2 32 virtualenv problems 3 99
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question