Can a char type be used like an array of bits?

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 1586 Views - Last Post: 23 February 2016 - 08:50 AM Rate Topic: -----

#1 EchoGecko   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 82
  • Joined: 19-July 15

Can a char type be used like an array of bits?

Posted 18 February 2016 - 04:46 AM

In C++...

I'm trying to figure out how I would read from a char as 1's and 0's. What I want to do is iterate through all the values that a char can represent (so if it's 8bits long, it should represent a number as high as 2 to the power of 8?). For each of these values, I would like to print out the binary representation. Another way of wording this would be to say I'm trying to generate the numbers 0-maximum in binary from in order.

I then would like to be able to use those binary values like bool arrays, so for e.g. being able to iterate through each bit of the byte representing a char.

Lastly, can I do this with a type that would hold 12 bits instead of 8?

This post has been edited by EchoGecko: 18 February 2016 - 04:47 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Can a char type be used like an array of bits?

#2 lisperati   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 25
  • Joined: 16-February 13

Re: Can a char type be used like an array of bits?

Posted 18 February 2016 - 05:01 AM

Hi

A char has an ascii code which you can convert to binary.
When you read the char just use the %d format conversion and it will give you the number.

Regards
Was This Post Helpful? 1
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Can a char type be used like an array of bits?

Posted 18 February 2016 - 05:01 AM

Sounds cool: what have you tried?

Note, to read, say, the third bit, you can do something like:
char x = 5;
cout << (x & 4) << endl;



To be clear, this will return 4 if the bit at 100 ( which is 4 ) is lit and 0 otherwise. Because 5 is 101, this should get you 4. Note, in C++ the numeric value 0 is false, all other values are true.

Here's an exercise: print out the binary representation of a char value. If you can do that, you'll have solved your problem.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#4 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 945
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Can a char type be used like an array of bits?

Posted 18 February 2016 - 05:24 AM

Since you're using C++, use std::bitset. It supports all of the things you're trying to do without falling back on old "C" constructs. You can specify as many or as few bits as you like, and there's no need for 'conversions' when using streams such as cin/cout.

#include <iostream>
#include <bitset>

int main()
{
    const int position3rdBit = 2;

    std::bitset<8> myBitset = 0b01011100;
    bool the3rdBit = myBitset.test(position3rdBit);
    std::cout << "The complete bitset: " << myBitset << std::endl;

    if (the3rdBit == true)
    {
        std::cout << "The 3rd bit is 1." << std::endl;
    }

    std::cout << "Now changing the 3rd bit to false..." << std::endl;
    myBitset.set(position3rdBit, false);
    the3rdBit = myBitset.test(position3rdBit);
    std::cout << "The complete bitset again: " << myBitset << std::endl;

    if (the3rdBit == false)
    {
        std::cout << "The 3rd bit is now 0." << std::endl;
    }
} 

Was This Post Helpful? 1
  • +
  • -

#5 EchoGecko   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 82
  • Joined: 19-July 15

Re: Can a char type be used like an array of bits?

Posted 18 February 2016 - 05:54 AM

View PostBench, on 18 February 2016 - 05:24 AM, said:

Since you're using C++, use std::bitset. It supports all of the things you're trying to do without falling back on old "C" constructs. You can specify as many or as few bits as you like, and there's no need for 'conversions' when using streams such as cin/cout.


Thanks, I think this will be very useful. Just one issue... The reason I wanted to do the conversions from char to bit sets was so that I'd have an indexing system for generating those bitsets in realtime. My actual goal here is to be able to generate all possible combinations of 0's and 1's of bit sets of any size (size has been 8 bits for the purposes of our examples so far).

For example, here is a sequence of 7 bits: 1,1,0,1,1,1,0
This sequence holds significance for my purposes. There are other sequences that hold significance too. 0,0,0,0,0,0,0 is a completely useless sequence for my purposes on the other hand. Now, basically I realized that a set of bits could also mean a single number if read as a byte, so if I were able to generate all possible combinations of bits by iterating through chars in numeric order from 0, I would always produce the binary sequences in the same order, so then I would simply make note of the char values that corresponded to significant binary sequences. I'm trying to create a dictionary of these significant sequences but without having to hard code them all.

This post has been edited by EchoGecko: 18 February 2016 - 06:13 AM

Was This Post Helpful? 0
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Can a char type be used like an array of bits?

Posted 18 February 2016 - 07:29 AM

View PostEchoGecko, on 18 February 2016 - 07:54 AM, said:

so then I would simply make note of the char values that corresponded to significant binary sequences. I'm trying to create a dictionary of these significant sequences but without having to hard code them all.


Interesting. You may ( or may not, not sure ) find Huffman coding useful reading: https://en.wikipedia.../Huffman_coding

The essence of a Huffman encoding is building a binary tree. However, the values are navigated to using binary values. Sounds like you may be doing both of those.
Was This Post Helpful? 1
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7445
  • View blog
  • Posts: 25,083
  • Joined: 05-May 12

Re: Can a char type be used like an array of bits?

Posted 18 February 2016 - 07:39 AM

Interesting insight... based on the OP's other thread, I thought that he was in the beginnings of working out some kind of Gray code.
Was This Post Helpful? 0
  • +
  • -

#8 EchoGecko   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 82
  • Joined: 19-July 15

Re: Can a char type be used like an array of bits?

Posted 18 February 2016 - 07:42 AM

View Postbaavgai, on 18 February 2016 - 05:01 AM, said:

Sounds cool: what have you tried?

Note, to read, say, the third bit, you can do something like:
char x = 5;
cout << (x & 4) << endl;



To be clear, this will return 4 if the bit at 100 ( which is 4 ) is lit and 0 otherwise. Because 5 is 101, this should get you 4. Note, in C++ the numeric value 0 is false, all other values are true.

Here's an exercise: print out the binary representation of a char value. If you can do that, you'll have solved your problem.

Hope this helps.


I have tried using this but honestly I can't quite get my head what this code is doing. What exactly do you mean by a bit being lit?
Was This Post Helpful? 0
  • +
  • -

#9 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Can a char type be used like an array of bits?

Posted 18 February 2016 - 07:56 AM

View PostEchoGecko, on 18 February 2016 - 09:42 AM, said:

What exactly do you mean by a bit being lit?


Sorry, I meant 1. Bits are boolean states: 0 or 1. Or, another way to see them as OFF and ON. Indeed, in the deep down depths of the computer brain, you can imagine switches being flipped as processes are begin done, off-on, unlit-lit. I apologize for the confusion.

So, in the above:
  0000 0101
& 0000 0100
-----------
= 0000 0100



Here, doing an AND where both bits are 1 will get you 1, else 0. Further,
  0000 0101
& 0000 0010
-----------
= 0000 0000



Doing the AND where no bits are shared gets zero. By using values that have only one bit "on" (1), you can figure out if the value you are ANDing against also has that bit on.
Was This Post Helpful? 0
  • +
  • -

#10 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Can a char type be used like an array of bits?

Posted 18 February 2016 - 03:52 PM

Typical way of extracting bits is the following. You AND the data with a 1 left shifted by the desired bit position.

uint8_t data = 0x5;

int bit0 = data & (1 << 0);
int bit1 = data & (1 << 1);
int bit2 = data & (1 << 2);
int bit3 = data & (1 << 3); 
//etc...


Was This Post Helpful? 0
  • +
  • -

#11 EchoGecko   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 82
  • Joined: 19-July 15

Re: Can a char type be used like an array of bits?

Posted 18 February 2016 - 09:48 PM

View PostSkydiver, on 18 February 2016 - 07:39 AM, said:

Interesting insight... based on the OP's other thread, I thought that he was in the beginnings of working out some kind of Gray code.


If you are interested in what I'm doing, I'm representing musical scales as binary sequences. The most common musical scale in Western music is known as the major scale, and this is what '1,1,0,1,1,1,0 signifies. In other words, a 0 represents moving up one note (a semi-tone) from the previous note (the root note that the scale starts on), and 1 represents moving up two notes(a tone) from the previous note. So for e.g. if you had root note of 60 (the MIDI note value for middle C), the 1,1,0,1,1,1,0 scale pattern would ultimately output a scale of these notes: 60, 62, 64, 65, 67, 69, 71, 72 which in MIDI is C,D,E,F,G,A,B,C.

My other thread related to calculating the modes of a given scale. Ultimately I am trying to create a device that can use all mathematically possible musical scales without hard coding the values representing those scales. Sadly I know way more about music and sound than I do about programming... Couldn't do this stuff without you guys!
Was This Post Helpful? 0
  • +
  • -

#12 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Can a char type be used like an array of bits?

Posted 19 February 2016 - 07:27 AM

View PostEchoGecko, on 18 February 2016 - 11:48 PM, said:

In other words, a 0 represents moving up one note (a semi-tone) from the previous note (the root note that the scale starts on), and 1 represents moving up two notes(a tone) from the previous note. So for e.g. if you had root note of 60 (the MIDI note value for middle C), the 1,1,0,1,1,1,0 scale pattern would ultimately output a scale of these notes: 60, 62, 64, 65, 67, 69, 71, 72


I couldn't help but fiddle with this. And, since you've already been given code:
char x = 0b1101110;
int n = 60;
std::cout << n;
while (x) {
    n += (x & 1) + 1;
    std::cout << ", " << n;
    x >>= 1;
}
std::cout << std::endl;



The problem is that you can't really express a stop, can you? Other types of data representations would be far easier to work with. What if you find you need a third value later? ( I think you do, with the stop. ) Think about the problem before you think about some final representation. Then, if needed, you can encode the more programmer friendly structure down to something numeric.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7445
  • View blog
  • Posts: 25,083
  • Joined: 05-May 12

Re: Can a char type be used like an array of bits?

Posted 19 February 2016 - 07:46 AM

What needs to be stored are:
struct Scale
{
    int  RootNote;
    char AscendingSteps;
    char DescendingSteps;
}



I am not knowledgeable about music and all I know is what I've scanned from Wikipedia. You may not need the DescendingSteps depending on whether you'll support the use case for some scales where it maybe different whether you are going up or going down the scale.
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7445
  • View blog
  • Posts: 25,083
  • Joined: 05-May 12

Re: Can a char type be used like an array of bits?

Posted 19 February 2016 - 07:54 AM

For now, let's ignore DescendingSteps. To get all the variations of AscendingSteps you'll simply need the following pseudo-code:
Scale scales[128];

for (pattern = 0; pattern < 128; pattern++)
{
    scales[pattern].AscendingPattern = pattern;
}



I don't know enough about musical theory to know how the root notes are chosen. For example, or the major scales, this looks pretty random to me unless somebody can point out what the pattern is:
Posted Image

Follow-up: Before somebody points out that for C-Major, the C note is the starting point, let me explain. If all you have is the step pattern of 1,1,0,1,1,1,0 (or W,W,H,W,W,W,H) how do you decide that you go from 62 © to 64 (D)? Wouldn't choosing to go from 63(C#) to 65(D#) been an equally good choice?

This post has been edited by Skydiver: 19 February 2016 - 08:02 AM
Reason for edit:: Added quick follow up.

Was This Post Helpful? 0
  • +
  • -

#15 EchoGecko   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 82
  • Joined: 19-July 15

Re: Can a char type be used like an array of bits?

Posted 19 February 2016 - 10:08 AM

View PostSkydiver, on 19 February 2016 - 07:54 AM, said:

Follow-up: Before somebody points out that for C-Major, the C note is the starting point, let me explain. If all you have is the step pattern of 1,1,0,1,1,1,0 (or W,W,H,W,W,W,H) how do you decide that you go from 62 © to 64 (D)? Wouldn't choosing to go from 63(C#) to 65(D#) been an equally good choice?



The root note is determined by realtime MIDI input from a controller device or pattern sequencer. Across all octaves this input value is converted to a value ranging from 0-11 to represent the 12 notes The scale dictionaries I'm writing build the MIDI notes in the scale across all octaves based on whatever the note input is at any given time. It's designed to be adaptable like that.

The table you posted is pretty confusing to look at but it's showing which notes belong to the major scale in all 12 musical keys.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2