Troubleshooting an 0x80 Mask for Char Bit Display

In summary, the conversation discusses a code for displaying the bits in a char and seeks assistance in fixing a problem with the code. One solution is suggested and a different one is provided, which is found to work but alters the original testvalue. The conversation concludes with the realization that the original problem was caused by the declaration of the variable "mask" as "char".
  • #1
Hypercase
62
0
PHP:
mask =  0x80;
for(i = 0; i < 8; i++)
{
	if((testvalue & mask)!= 0)
            {			
                cout<<"1";
            }
	else
            {
	cout<<"0";
            }
	mask = mask >> 1;

}
The code displayed was for displaying the bits in a char,however it doesn't work. Please help me with the code.
Thanx.
 
Computer science news on Phys.org
  • #2
Is this homework?

This code appears by inspection to be correct. Is mask the same data type as testvalue?

- Warren
 
  • #3
PHP:
char testvalue,mask ;

No it isn't HW.
 
  • #4
PHP:
for(i = 0; i < 8; i++)
{
      if((test & 0x80)!= 0)
      {
       cout<<"1";
      }
      else
      {
       cout<<"0";
      }
    test = test << 1;

 }
This code works well however the testvalue is altered. I didnt want that to happen ,and I therefore wrote the code displayed in post1, where the mask is shifted.
 
  • #5
Hypercase said:
it doesn't work.

Please be more specific: what exactly doesn't work? As chroot said, the code looks OK.
 
  • #6
You might find this code interesting (this is how I'd probably write it, I'd just add some more checks in real code).

Code:
#include <iostream>
#include <cassert>

using namespace std;

template <typename T>
void print_bits(const T& value, int to_go = sizeof(T) * 8)
{
        if (to_go == 0) return;
        assert (to_go > 0);
        bool last = value & T(1);
        print_bits(value >> 1, to_go - 1);
        cout << last;
}

int main()
{
        print_bits('a'); cout << endl;
        print_bits(-17); cout << endl;
        print_bits(unsigned(-17)); cout << endl;
        print_bits(short(-17)); cout << endl;
}

Run it and see what it prints. BTW, I think the "*8" part is not guaranteed by the standard to be portable but I don't know any platform where this would be a problem.
 
  • #7
Ah, I know why it doesn't work! if you declared "mask" as "char" then 0x80 is actually a negative value and the sign bit is preserved. you should have written those declarations in the first piece of code :) I've barely nticed you mantioned it latter.
 

FAQ: Troubleshooting an 0x80 Mask for Char Bit Display

What is an 0x80 mask for char bit display?

An 0x80 mask is a hexadecimal value that is used in computer programming to manipulate individual bits in a character. It is often used to turn on or off a specific bit in a character, which can then be displayed on a screen or other output device.

Why would I need to troubleshoot an 0x80 mask for char bit display?

You may need to troubleshoot an 0x80 mask if you are encountering issues with displaying characters correctly on a screen or output device. This could be due to errors in the code or incorrect use of the mask.

How do I troubleshoot an 0x80 mask for char bit display?

To troubleshoot an 0x80 mask, you can start by checking your code for any errors or mistakes. You can also use a debugger to step through your code and see where the issue may be occurring. Additionally, you can consult online resources or ask for help from other programmers.

Can I use an 0x80 mask for char bit display in any programming language?

Yes, the 0x80 mask can be used in most programming languages that support hexadecimal values. However, the syntax for using the mask may vary slightly between languages.

Are there any common mistakes to avoid when using an 0x80 mask for char bit display?

Yes, some common mistakes to avoid include using the mask on the wrong data type, forgetting to apply the mask to the character before displaying it, and incorrectly setting the bit to be turned on or off in the mask.

Similar threads

Back
Top