hi...
let's say i got a char a = 'A';
and an arry of bool b[8];
how to convert variable a into binary and store it into b[]?
Thanks
hi...
let's say i got a char a = 'A';
and an arry of bool b[8];
how to convert variable a into binary and store it into b[]?
Thanks
A for loop, and use of the >>(shift-right) and &(bitwise-and) operators
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
can you demostrate with some coding?
Code:#include <stdio.h> #include <stdlib.h> #include <limits.h> void printBits(unsigned long val){ int i, end = sizeof(unsigned long) * 8; unsigned long tmp = (ULONG_MAX / 2) + 1; for (i=0; i<end; i++){ if (tmp & val) putc('1', stdout); else putc('0', stdout); tmp >>= 1; } } int main(){ int number = 0; char buf[BUFSIZ]; while (1){ printf("Please enter a number to convert to binary: "); fgets(buf, BUFSIZ, stdin); rewind(stdin); number = atoi(buf); printBits(number); printf("\n"); } return 0; }
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
For example:
num & 1 is true if the 1st bit is 1.
Of course if it isnt true, then the 1st bit is 0.
num = num >> 1 would then bitwise shift num such that the 2nd bit is now the 1st bit.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Excellent code, mitakeet. Except I don't see BUFSIZ declared anywhere. Or is it part of one of the header files?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
ok.. my code:
ok... now how do we convert from bits back to the original variable value?Code:void main() { unsigned char a = 1; unsigned char b[8]; char mask = 1; int i; for(i = 8; i > 0; i--) { printf("%d", a & mask); b[i - 1] = a & mask; a >>= 1; } printf("\n"); for(i = 0; i < 8; i++) { if(b[i] == 1) { printf("1"); } else { printf("0"); } } }
BUFSIZ is declared in <stdio.h> and is typically around 512 bytes.
If you want to put it back together, you |= with the appropriate bit mask.
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
coding example please....
int value = 0;
int mask1 = 1;
value |= mask1;//now first (lowest order) bit is set
int mask2 = 2
value |= mask2;//now first and second low order bits are set
int mask3 = 4;....
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
based on my program the bits is store on b[8];
i try:
but not working......Code:void main() { unsigned char a = 'a'; unsigned char c = 0; unsigned char b[8]; char mask = 1; int i; for(i = 8; i > 0; i--) { printf("%d", a & mask); b[i - 1] = a & mask; a >>= 1; } printf("\n"); for(i = 0; i < 8; i++) { c |= b[i]; c <<= 1; if(b[i] == 1) { printf("1"); } else { printf("0"); } } printf("\n%c\n", c); }
Last edited by draggy; 07-12-2005 at 12:53 PM.
What does it output?
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
output:
ThanksCode:10000110 01100001 ┬
01100001 is the ASCII code for 'a'.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
yes... the binary ASCIIOriginally Posted by dwks