bits

It is possible to write less than 1 byte to a file

佐手、 提交于 2019-12-04 00:08:28
As far as I know the smallest unit in C is a byte . Where does this constraint comes from? CPU? For example, how can I write a nibble or a single bit to a file? no, you can't... files are organized in bytes, it's the smallest piece of data you can save. And, actually, that 1 byte will occupy more than 1 byte of space, in general. Depending on the OS, the system file type, etc, everything you save as a file will use at least one block. And the block's size varies according to the file system you're using. Then, this 1-bit will be written as 1 - byte and can occupy as much as 4kB of your disk.

Converting a String representation of bits to a byte

自古美人都是妖i 提交于 2019-12-03 12:29:11
I'm just beginning to learn about file compression and I've run into a bit of a roadblock. I have an application that will encode a string such as "program" as a compressed binary representation "010100111111011000" (note this is still stored as a String). Encoding g 111 r 10 a 110 p 010 o 011 m 00 Now I need to write this to the file system using a FileOutputStream , the problem I'm having is, how can I convert the string "010100111111011000" to a byte[] / byte s to be written to the file system with FileOutputStream ? I've never worked with bits/bytes before so I'm kind of at a dead end here

Size of a bitfield member?

前提是你 提交于 2019-12-03 11:31:21
问题 Would anyone know how to extract the size of a bit-field member. The below code naturally gives me the size of an integer, but how do I find out how many bits or bytes are in mybits.one ? I've tried sizeof(test.one) but which clearly won't work. I realize this is a measure of bits: #include <iostream> using namespace std; int main() { struct mybits { unsigned int one:15; }; mybits test; test.one = 455; cout << test.one << endl; cout << "The size of test.one is: " << sizeof(test) << endl; }

bitParity - Finding odd number of bits in an integer

元气小坏坏 提交于 2019-12-03 09:03:23
I have to create a function bitParity(int x) that takes an integer and returns 1 if there is an odd number of 0 's in the bit form of x , and 0 otherwise. Ex: bitParity(5) = 0, bitParity(7) = 1 However, this is difficult as I can only use bit operators on this problem ( ! ˜ & ˆ | + << >> are the only legal ones). That means, no loops, if-then , or anything of the sort. Constants can be used. So far, what I have doesn't work, but I figured that I should shift the bits of the integer 16 , 8 , and 4 times and XOR the remaining integers. Can anyone offer some advice? Thanks. This is properly

Bit operations (C++)

僤鯓⒐⒋嵵緔 提交于 2019-12-03 07:36:44
问题 Recently I had a question on the interview - I was asked to compare bitwise operations in performance terms. Like, give a brief description of the performance of different bit operations. I guess this question could is pretty general and pretty machine-specific, but I also think there should be some general rules about this, which you have to mention (and I didn't :). So - what would you answer? I probably should also say, that it may be a good idea to compare their performance in C (or C++,

How do I count the number of zero bits in an integer?

我只是一个虾纸丫 提交于 2019-12-03 07:30:22
问题 How would i go about finding the number of 'zero' bits in C++. Suppose I have an integer; int value = 276; For which I have the bits 100010100, but how do I count the zeros? 回答1: The easiest most naive way is to just iterate over the bits and count: size_t num_zeroes = 0; for(size_t i = 0; i < CHAR_BIT * sizeof value; ++i) { if ((value & (1 << i)) == 0) ++num_zeroes; } There are all number of better (for different values of "better") ways, but this is quite clear, very terse (code-wise), and

Non-restoring division algorithm

一曲冷凌霜 提交于 2019-12-03 06:53:12
Does anyone know the steps for dividing unsigned binary integers using non-restoring division? It's hard to find any good sources online. i.e if A = 101110 and B = 010111 how do we find A divided by B in non-restoring division? What do the registers look like in each step? Thanks! (My answer is a little late-reply. But I hope it will be useful for future visitors) Algorithm for Non-restoring division is given in below image : In this problem, Dividend (A) = 101110, ie 46, and Divisor (B) = 010111, ie 23. Initialization : Set Register A = Dividend = 000000 Set Register Q = Dividend = 101110 (

Is there a way to access individual bits with a union?

牧云@^-^@ 提交于 2019-12-03 05:54:56
问题 I am writing a C program. I want a variable that I can access as a char but I can also access the specific bits of. I was thinking I could use a union like this... typedef union { unsigned char status; bit bits[8]; }DeviceStatus; but the compiler doesn't like this. Apparently you can't use bits in a structure. So what can I do instead? 回答1: Sure, but you actually want to use a struct to define the bits like this typedef union { struct { unsigned char bit1 : 1; unsigned char bit2 : 1; unsigned

Grabbing n bits from a byte

China☆狼群 提交于 2019-12-03 02:25:41
问题 I'm having a little trouble grabbing n bits from a byte. I have an unsigned integer. Let's say our number in hex is 0x2A, which is 42 in decimal. In binary it looks like this: 0010 1010. How would I grab the first 5 bits which are 00101 and the next 3 bits which are 010, and place them into separate integers? If anyone could help me that would be great! I know how to extract from one byte which is to simply do int x = (number >> (8*n)) & 0xff // n being the # byte which I saw on another post

Size of a bitfield member?

大兔子大兔子 提交于 2019-12-03 02:01:14
Would anyone know how to extract the size of a bit-field member. The below code naturally gives me the size of an integer, but how do I find out how many bits or bytes are in mybits.one ? I've tried sizeof(test.one) but which clearly won't work. I realize this is a measure of bits: #include <iostream> using namespace std; int main() { struct mybits { unsigned int one:15; }; mybits test; test.one = 455; cout << test.one << endl; cout << "The size of test.one is: " << sizeof(test) << endl; } Runtime solution, the idea from this discussion: http://social.msdn.microsoft.com/Forums/en-US/7e4f01b6