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.
 

1. 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.

2. 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.

3. 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.

4. 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.

5. 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

  • Programming and Computer Science
Replies
5
Views
881
  • Engineering and Comp Sci Homework Help
Replies
2
Views
942
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
32
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
3
Replies
89
Views
4K
Back
Top