I have a project to do assembler and simulator in c++ with virtual memory (of characters) 128KB
I just wondering how could I convert char to it's corresponding bits?
how can I reset the bits of char type?
I have a project to do assembler and simulator in c++ with virtual memory (of characters) 128KB
I just wondering how could I convert char to it's corresponding bits?
how can I reset the bits of char type?
You'll want to know the endian-ness of your target system. C and C++ define the width of a char
differently; you'll want to make sure you are stepping through the correct amount of offsets. Then it's just a simple matter of testing each bit offset against a bitmask. This is some code from an old Code Review question of mine; modified to :
#include <stdio.h>
char set_bit(const char x, const char offset, const int value) {
return value ? x | (1 << offset) : x & ~(1 << offset);
}
char get_bit(const char x, const char offset) {
return (x & (1 << offset)) != 0;
}
int main(void) {
int max = sizeof(char) * 8 - 1;
char x = 0;
int count;
for (count = 0; count < max; count += 2)
x = set_bit(x, count, 1);
for (count = max; count >= 0; count--)
printf("%d ", (int) get_bit(x, count));
printf("\n");
return 0;
}
Output:
$ g++ main.cpp -o main && ./main
0 1 0 1 0 1 0 1