the code displayed was for displaying the bits in a char,however it doesn't work. Please help me with the code.Code:mask = 0x80; for(i = 0; i < 8; i++) { if((testvalue & mask)!= 0) { cout<<"1"; } else { cout<<"0"; } mask = mask >> 1; }
Thanx.
the code displayed was for displaying the bits in a char,however it doesn't work. Please help me with the code.Code:mask = 0x80; for(i = 0; i < 8; i++) { if((testvalue & mask)!= 0) { cout<<"1"; } else { cout<<"0"; } mask = mask >> 1; }
Thanx.
So to do a full byteCode:// Assuming you want to look at bit 7 int bit_num = 7; // Shifts the number so the bit you want is on the left // so we can use bitwise and on it and it will either be 1 or 0 int the_bit = ( ( testvalue >> bit_num ) & 1 ); cout << the_bit;
[edit]Code:for ( int loop = 7; loop >= 0; loop-- ) { int the_bit = ( testvalue >> loop ) & 1; cout << the_bit; }
Also this really should have been posted in the c++ board.
Last edited by Vicious; 09-15-2004 at 02:00 AM.
What is C++?
>Also this really should have been posted in the c++ board.
why?
Thanx for the program, i hav another one working too. Its just that I wanted to know what was wrong with the code I posted above.
Also by doing "testvalue >> loop " I'm changing the testvalue which may affect other values that operate on test value.
Hence i wanted to shift the bits in the mask = 0x80.
@sand_man: Because his code is C++ code...
@Hypercase:
Nope test value will still have its original value because you havent re-assigned it. For it to change you would have to do something like testvalue >>=loopAlso by doing "testvalue >> loop " I'm changing the testvalue which may affect other values that operate on test value.
And Ive been looking at your code and I bet it always outputs 0's doesnt it? I think it has something to do with how you assign mask.. Im not sure yet... I am still trying to figure it out
[edit]
I tried this
And it worked... odd. Maybe if you show some more code I can figure it out.Code:#include <iostream> int main ( void ) { int mask = 0x80; char testvalue = 0xFA; for ( int loop = 0; loop < 8; loop++ ) { if ( ( testvalue & mask ) != 0 ) { std::cout << "1"; } else std::cout << "0"; // Same thing as mask = mask >> 1 mask >>= 1; } return 0; }
Last edited by Vicious; 09-15-2004 at 02:40 AM.
What is C++?
Thanx Vicious, that was very helpful.
You can see what's going on if you display the bit pattern:Originally Posted by Vicious
On the left is the value of the mask. The bit pattern of the mask is in the middle. On the right is the result of it.Code:#include <stdio.h> void draw ( int v, int n ) { int x; for( x = 0; x < n; x++ ) { printf("%d", (v & (1 << ((n-1)-x))) != 0 ); } } int main ( void ) { int mask = 0x80; int loop; char testvalue = 0xFA; for( loop = 0; loop < 8; loop++ ) { printf("%3d ", mask ); draw( mask, 8 ); printf(": %d\n", (testvalue &mask) != 0 ); mask >>= 1; } return 0; } /* 128 10000000: 1 64 01000000: 1 32 00100000: 1 16 00010000: 1 8 00001000: 1 4 00000100: 0 2 00000010: 1 1 00000001: 0 */
[edit]
I changed the order the bits display (left to right, instead of right to left), so perhaps it'll be a bit clearer.
[/edit]
Quzah.
Last edited by quzah; 09-15-2004 at 11:20 AM.
Hope is the first step on the road to disappointment.
just for fun - here's an easy way to display an integer in any base (even user defined):
Code:char * tobase(int number, char * buffer, const char * digits) { size_t index, base = strlen(digits); char * b = buffer; if(base) { while(number) { index = number % base; *b++ = digits[index]; number /= base; } } *b = 0; return strrev(buffer); } int main(int, char ** argv) { int input, size = 256; char * dec = "0123456789", * hex = "0123456789ABCDEF", * oct = "01234567", * bin = "01", * base36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", buffer[size+1]; while(printf(" Enter an integer to convert >") && *fgets(buffer, size, stdin) != '\n') { input = atoi(buffer); printf(" Control: %d\n", input); printf(" Decimal: %s\n", tobase(input, buffer, dec)); printf(" Hexidecimal: %s\n", tobase(input, buffer, hex)); printf(" Octal: %s\n", tobase(input, buffer, oct)); printf(" Binary: %s\n", tobase(input, buffer, bin)); printf(" Base 36: %s\n", tobase(input, buffer, base36)); } return 0; }
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
I wouldn't exactly say any base. Since your method is dependant on a digit being only one byte, you will be limited to a maximum of 256-base (or is it 255-base?).
hmm, interesting point.Originally Posted by Frobozz
actually, I overlooked a couple of things, too: the loop is set up wrong and the function doesn't account for negative numbers!
lemme try again:
Code:char * tobase(int number, char * buffer, const char * digits) { size_t index, base = strlen(digits); char * b = buffer; int isNeg = 0; if(number < 0) { isNeg = 1; number = -number; } if(base) { do{ index = number % base; *b++ = digits[index]; number /= base; } while(number); } if(isNeg) { *b++ = '-'; } *b = 0; return strrev(buffer); }
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Originally Posted by Hypercase
What do you mean "doesn't work"? Are you sure that testvalue has the value that you think it has? Your code works perfectly for me.
Run the following and see what you get.
(Redirect the output to a file so that you can inspect the results, or change the outer loop to see whatever values you are interested in.)Code:#include <iostream> using namespace std; int main() { char testvalue; int mask; for (int j = -128; j <= 127; j++) { testvalue = j; cout << (int)testvalue << " decimal = "; mask = 0x80; for(int i = 0; i < 8; i++) { if((testvalue & mask)!= 0) { cout << "1"; } else { cout << "0"; } mask = mask >> 1; } cout << " binary" << endl; } return 0; }
Regards,
Dave
Last edited by Dave Evans; 09-16-2004 at 08:45 AM.
The code now works after this:
Code:#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <limits.h> #include <float.h> char * tobase(int number, char * buffer, const char * digits){ size_t index, base = strlen(digits); char * b = buffer; int isNeg = 0; if(number < 0){ isNeg = 1; number = -number; } if(base){ do{ index = number % base; *b++ = digits[index]; number /= base; } while(number); } if(isNeg){ *b++ = '-'; } *b = 0; return _strrev(buffer); } int main(int, char ** argv){ int input; int size = 256; char buffer[256]; char * dec = "0123456789", * hex = "0123456789ABCDEF", * oct = "01234567", * bin = "01", * ter = "012", * mayan = "0123456789ABCDEFGHIJKLMNOPQRST", * base36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; while(printf("Enter an integer to convert >") && *fgets(buffer, size, stdin) != '\n'){ input = atoi(buffer); printf("Control: %d\n", input); printf("Decimal: %s\n", tobase(input, buffer, dec)); printf("Hexidecimal: %s\n", tobase(input, buffer, hex)); printf("Octal: %s\n", tobase(input, buffer, oct)); printf("Binary: %s\n", tobase(input, buffer, bin)); printf("Tertiary: %s\n", tobase(input, buffer, ter)); printf("Mayan: %s\n", tobase(input, buffer, mayan)); printf("Base 36: %s\n", tobase(input, buffer, base36)); } return 0; }
Last edited by msummers; 04-14-2008 at 04:31 PM. Reason: Have fix the code