Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Is there a generic way to represent a number containing n bits using n-1 bits; e.g. showing 1001 which has 4 bits using XXX where X = {0|1} with 3 bits. Also, the mapping should be able to retrieve the original binary back without any collision.
This page is the most relevant reference that I have found so far which tries to count the number of bits, but not reduce the number of bits.
Edit: I knew this sounds impossible, but I'm curious if there exists a workaround to do so!
There are 2^n possible values with n bits, and 2^(n-1) with (n-1) bits. So you can't convert from the former to the latter losslessly.
If it were at all possible, you could also recursively represent your n-1 bits using n-2 bits, etc. Everything would be representable with 0 bits :)
You get misleaded by the page you linked, which explains that x &= x-1 removes a 1 in a bit string.
10100
& 10011
= 10000
You will be very clever if you manage to establish a one-to-one mapping between the 16 numbers
0000,0001,0010,0011,0100,0101,0110,0111,1000,1001,1010,1011,1100,1101,1110,1111
and the eight numbers
000,001,010,011,100,101,110,111.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
If we have data of 2 Tb, how to express this in 2 to the power of x? (to the closest one)
The answer needs to be a number in decimal. I feel that it is 40.99 but turns out to be wrong. I'm wondering how to solve this problem. Thanks!
The Metric System prefixes all represent powers of ten, and “tera” means 1012. So two terabytes is 2,000,000,000,000 bytes, not 2,199 billion bytes.
There are binary prefixes for powers of two. These replace the second syllable of the Metric System prefixes with “bi,” for “binary”. A tebibyte is 10244 = 240 bytes, and two tebibytes is 241 bytes = 2,199,023,255,552 bytes.
Given a number x, you can calculate what power of two it is as log2 x, that is, the base-two logarithm of x. If you do not have a base-two logarithm function, you can calculate it as log x / log 2, where “log” is a logarithm function that uses any base. (Dividing by log 2 effectively adjusts the base.)
log2 2,199,000,000,000 is about 40.9999847, and that is near 41. So, if a number of bytes is near 2,199 billion bytes, it is near 241 bytes.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am studying for an interview and found this question a bit bewildering. Would appreciate your advise.
What happens to the left side of an unsigned number after performing a bit-shift operation to the right?
1. the number will be filled with 1's on the left
2. the method of the numbers being filled on the left depends on the system
3. all answers are wrong
4. the number will be filled with 0's on the left
5. the left part of the number will be filled with the bits that were lost from the right
The answer is (4).
If it had been a signed int, then most compilers would fill the left bits with 1's if the sign bit is 1 (i.e. if the number is negative), or with 0's if the sign bit is 0. But this is implementation-defined.
(5) is there to trip you up if you happen to know some assembly. It would be correct if it were a rotate operation, which is not supported by the C language.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Forgive me if this is a duplicate, however I need someone to explain to me how to accomplish the following (the bitwise masking/shifting in C confuses me)
From what I understand I need to switch the first 4 bits with the last 4 bits, but I am unsure how to do so.
This is a question taken from a Midterm Exam that has been posted for Review/Study purposes
Question:
I have declared two unsigned char variables, a and b. Assume that a has been initialized. I would like to assign a value to b such that the lowest four bits of a are the highest four bits of b and the highest four bits of a are the lowest four bits of b.
Write a single line of C code that will accomplish this.
And if you can, explain it to me so I can better understand what is going on, thank you.
Follow these steps:
extract the 4 low order bits of a with a & 0x0F
shift them left by 4 with the << operator
extract the 4 high order bits of a with a & 0xF0
shift these right by 4 to bring them to the low order position with >>
combine these results with |.
add some parentheses to ensure correct precedence
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
What is the best way to count the number of 1's in an 32-bit integer x in C without using for or while loops, and without using constants greater than 0xFF?
What I thought of is shifting x 24 to the right and count how many 1's in the shifted integer and store that in a variable count. And then, shifting x 16 to the right and increment count by the number of 1's in the shifted integer, and so on.
So, any ideas of a better solution?
You can tabulate the number of 1's in all d bits numbers. This takes a table of 2^d entries, each not exceeding the value d (<255).
Now you can cut your number in slices of d bits and lookup the counts for all slices.
A good compromise between space/number of operations is probably with d=4 (8 slices, table size=16).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am looking for fast check sum algorithm that produce 1 byte checksum.
I checked CRC8 and Adler8, but I do not fully understand the samples.
Also different CRC8 implementations give different results.
In all cases I do not need anything that fancy.
CRC's are based on a type of finite field math, using polynomials with 1 bit coefficients (math modulo 2). An 8 bit CRC is the result of treating data as a very long polynomial dividend with 1 bit coefficients and dividing it by a 9 bit polynomial divisor, which produces an 8 bit remainder. Since 1 bit coefficients are used, add or subtract effectively become exclusive or. You don't really need to understand finite field math to implement a CRC, just use a lookup table or use an algorithm to generate the CRC.
You could just add up all the bytes into a 1 byte sum, and use that as a checksum. The advantage of a CRC is that if bytes are missing or out of order, it has a better chance of detecting that.