I have an input file that has 16 bit int addresses. For each entry in the list, I need to read in 8 bits for a "page number", and 8 bits for an "offset". Any idea how I could do this? I don't find any support for doing bitwise operations in c.
Asked
Active
Viewed 48 times
-2
-
You can use `fread` to read binary data. – Fiddling Bits Apr 06 '15 at 00:55
-
What language are you using? `C` or `C++`? – Galik Apr 06 '15 at 00:56
-
***I don't find any support for doing bitwise operations in c*** There are shifts, and, or, xor ... – drescherjm Apr 06 '15 at 00:56
1 Answers
2
Read your text file into an array of 16-bit elements, and then separate the high and the low portions like this:
uint16_t num;
uint8_t low = num & 0xFF;
uint8_t high = (num >> 8) & 0xFF;

Sergey Kalinichenko
- 714,442
- 84
- 1,110
- 1,523
-
how would you read in a byte from a string of digits though? I'm not exactly sure how to parse the string into numerical bytes. (I don't mean individual characters) – John Dodson Apr 06 '15 at 01:02
-
-
This doesn't seem to work, instead of getting two int values I get a bunch of random characters – John Dodson Apr 06 '15 at 04:48
-
@JohnDodson "random characters"? Probably, you are not printing them right? – Sergey Kalinichenko Apr 06 '15 at 07:30