2

I have the question of the title, but If not, how could I get away with using only 4 bits to represent an integer?

EDIT really my question is how. I am aware that there are 1 byte data structures in a language like c, but how could I use something like a char to store two integers?

anatolyg
  • 26,506
  • 9
  • 60
  • 134
papiro
  • 2,158
  • 1
  • 20
  • 29
  • A byte and store three digits 0 - 255 to be exact – Ed Heal Jun 21 '15 at 04:08
  • 1
    `1byte != 4bits` `1byte = 8bits` – JackV Jun 21 '15 at 04:09
  • 1
    Yes. As others have explained, a byte is 8 bits, not 4. Did you try anything? Also, please pick a language. You tagged this as c, c++, and Fortran. Those are three different languages. – elixenide Jun 21 '15 at 04:10
  • 4
    [BCD is a specific class of formatting.](https://en.wikipedia.org/?title=Binary-coded_decimal) Reading up on it will explain how it works. – user2864740 Jun 21 '15 at 04:12
  • Do people still use bcd – Ed Heal Jun 21 '15 at 04:15
  • 1
    @EdHeal: [Yes.](http://www.maximintegrated.com/en/products/digital/real-time-clocks/DS1308.html) – Ben Voigt Jun 21 '15 at 04:20
  • 2
    To simplify parsing and formatting logic. Did you click my link? Virtually every RTC in existence uses BCD encoding, that was the first Google hit and it turned out to be BCD (as expected) – Ben Voigt Jun 21 '15 at 04:25
  • You guys, I am well aware that a byte is 8 bits. If you could read, I was asking about storing TWO integers in a 1 byte data structure OR one integer in a 4 bit (nibble) data structure. But thanks for down voting me. – papiro Jun 21 '15 at 04:27
  • 3
    @papiro a byte can store 256 values , whereas 2-decimal-digit numbers only have 100 possibilities, so obviously it is possible. Similarly , 4 bits has 16 possible values but there are only 10 possible digits. so you have thousands of ways of doing what you want. Can you ask a more specific question? – M.M Jun 21 '15 at 04:27
  • 2
    `char ch = 99;` stores the 2-digit number `99` in an 8-bit char – M.M Jun 21 '15 at 04:30
  • 3
    Matt, it's as easy as that?? – papiro Jun 21 '15 at 04:31
  • @papiro, `char` is an integer type so why not? – tangrs Jun 21 '15 at 04:32
  • 1
    @user2864740 The user himself actually reference BCD in his previous (then Fortran only) question http://stackoverflow.com/questions/30959760/how-can-i-implement-bcd-in-fortran – Vladimir F Героям слава Jun 21 '15 at 07:39
  • 1
    @papiro I changed the title to what I understood you wanted to do. Please clarify if I understood it wrong (use [edit]). – anatolyg Jun 21 '15 at 07:54
  • Where does this requirement cone from? There are multiple solutions and depending on what you want to do one might be better than the other. – MikeMB Jun 21 '15 at 08:06
  • Thank you so much everyone. Definitely learning a lot from this thread! – papiro Jun 21 '15 at 15:52

2 Answers2

8

In C or C++ you can use a struct to allocate the required number of bits to a variable as given below:

#include <stdio.h>
struct packed {
    unsigned char a:4, b:4;
};
int main() {
    struct packed p;
    p.a = 10;
    p.b = 20;
    printf("p.a %d p.b %d size %ld\n", p.a, p.b, sizeof(struct packed));
    return 0;
}

The output is p.a 10 p.b 4 size 1, showing that p takes only 1 byte to store, and that numbers with more than 4 bits (larger than 15) get truncated, so 20 (0x14) becomes 4. This is simpler to use than the manual bitshifting and masking used in the other answer, but it is probably not any faster.

amaurea
  • 4,950
  • 26
  • 35
kravi
  • 747
  • 1
  • 8
  • 13
6

You can store two 4-bit numbers in one byte (call it b which is an unsigned char).

Using hex is easy to see that: in b=0xAE the two numbers are A and E.

Use a mask to isolate them:

a = (b & 0xF0) >> 4

and

e = b & 0x0F

You can easily define functions to set/get both numbers in the proper portion of the byte.

Note: if the 4-bit numbers need to have a sign, things can become a tad more complicated since the sign must be extended correctly when packing/unpacking.

Pynchia
  • 10,996
  • 5
  • 34
  • 43
  • As far as signed numbers are concerned, I 'd just convert them to unsigned ones by adding 8 before they get packed and subtracting 8 after they got unpacked. – MikeMB Jun 21 '15 at 08:04