c question

cfleck

tired
I have a situation where I want to have a string of bits *much* larger than what is allowed (32 bits I believe). I am wondering if there is a way to combine several of these 32 bit units to create one larger unit.

I need to be able to to various operations on said unit like bit shifting and the like. The problem is, 32 bits isn't even close to what I need. Any ideas on how to go about it?
 

boyfarrell

Registered
On a related topic.

Is double equivalent to long or long long?

If I was to have an 'unsigned double' data type what would happen if it got sent a negative value, does it default to zero or just give out an error?
 

kainjow

Registered
No double stores floating point values (i.e. the part after the decimal place) and chars/shorts/ints/longs store only whole numbers. The difference between a float and a double is that a double is twice as precise.

When you use signed integers (chars, shorts, ints, longs), the last bit (the most significant bit) is reserved to represent negative or positive for the number. When using unsigned integers, this bit is used to store the number like normal. So if you don't need negative numbers, use unsigned integers because you can store 2x as many values in the same amount of bits.

For example, a char is 8 bits. So is an unsigned char. But an unsigned char can hold values up to 2^8-1, but signed chars can only hold values up to 2^7-1 because the last bit is reserved for negative or positive (0=positive, 1=negative). This doesn't make much different with today's average desktop or laptop computers, but if you're working in embedded systems where your memory is limited, or working with binary files where every bit is important, you need to know the difference.

Oh, and doubles don't have unsigned or signed. Only integers do.
 

boyfarrell

Registered
It's a shame that there isn't a unsigned float type. And yeah, lookling at that article I can see why!
 

lurk

Mitä?
You cam make them unsigned just ignore the sign bit ;) But really what would that get you? If you could use that bit elsewhere one extra bit of exponent or mantissa is not going to be worth squat.
 

lurk

Mitä?
You could use something like GMP but I don't know if that will give you all the bitwise operations need, lots of bit level operations don't translate. What would a rotate right mean for an arbitrary length int?
 

PBG4 Dude

Registered
Almost sounds like you'll need to code your own bit-shifting routines if you're going beyond the 32/64 bits afforded by the registers in your processor.
 

cfleck

tired
I'm not using it to represent a number, exactly. More so I've come up with a scheme to represent something else as 1s and 0s. And while I don't need all the bit manipulations available for normal bit-strings, the left and right shift are necessary and fast. That is why I was looking for the arbitrary length.
 

Captain Code

Moderator
Staff member
Mod
Bit shifting is only fast because it's built into the CPU. You're limited to what the CPU supports for this.

You'd have to split it up into chunks and then work with it.
 
Top