Troubleshooting an 0x80 Mask for Char Bit Display

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a code snippet intended to display the bits of a character using a mask. Participants explore the functionality of the code, its correctness, and potential issues related to data types and variable manipulation.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a code snippet for displaying bits in a char but reports that it does not work as intended.
  • Another participant questions whether the code is homework and inspects the code for correctness, asking if the mask is the same data type as testvalue.
  • A participant clarifies that the code is not homework and specifies the data types used for testvalue and mask.
  • Another code snippet is provided that successfully displays bits but alters the original testvalue, which the original poster wanted to avoid.
  • A participant requests more specifics on what "doesn't work" in the original code, noting that it appears correct.
  • A different approach to printing bits is suggested, including a template function that recursively prints bits, with a note about potential portability issues regarding the multiplication by 8.
  • One participant identifies a potential issue with the original code, suggesting that declaring "mask" as "char" could lead to problems due to the sign bit being preserved when using 0x80.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the original code's functionality, with some suggesting it is correct while others point out potential issues. The discussion remains unresolved regarding the specific reasons for the original code's failure.

Contextual Notes

There are unresolved questions regarding the data types of variables and the implications of using signed versus unsigned types in the context of bit manipulation.

Hypercase
Messages
62
Reaction score
0
PHP:
mask =  0x80;
for(i = 0; i < 8; i++)
{
	if((testvalue & mask)!= 0)
            {			
                count<<"1";
            }
	else
            {
	count<<"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
Is this homework?

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

- Warren
 
PHP:
char testvalue,mask ;

No it isn't HW.
 
PHP:
for(i = 0; i < 8; i++)
{
      if((test & 0x80)!= 0)
      {
       count<<"1";
      }
      else
      {
       count<<"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.
 
Hypercase said:
it doesn't work.

Please be more specific: what exactly doesn't work? As chroot said, the code looks OK.
 
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.
 
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.
 

Similar threads

  • · Replies 24 ·
Replies
24
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
5
Views
2K
Replies
17
Views
6K
  • · Replies 32 ·
2
Replies
32
Views
4K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K