bit

Number of bits in basic data type

杀马特。学长 韩版系。学妹 提交于 2019-12-11 06:15:35
问题 Here's a couple of thoughts.I'm learning so there might be mistake(s) and even missing some basics. sizeof operator returns number of bytes. number of bits in byte is not constant value(correct me but it's number of bits char has). I want to know how many bits variable occupies, and sizeof won't tell me that without making assumptions about number of bits in char. So I came up with this piece of (probably unnecessary) code: #include <stdio.h> #include <math.h> #include <limits.h> int main

Java like function getLeastSignificantBits() & getMostSignificantBits in Python?

孤街浪徒 提交于 2019-12-11 05:42:10
问题 Can someone please help me out in forming an easy function to extract the leastSignificant & mostSignificant bits in Python? Ex code in Java: UUID u = UUID.fromString('a316b044-0157-1000-efe6-40fc5d2f0036'); long leastSignificantBits = u.getLeastSignificantBits(); private UUID(byte[] data) { long msb = 0; long lsb = 0; assert data.length == 16 : "data must be 16 bytes in length"; for (int i=0; i<8; i++) msb = (msb << 8) | (data[i] & 0xff); for (int i=8; i<16; i++) lsb = (lsb << 8) | (data[i]

Bit Counting in C similar to bit twiddling hack

佐手、 提交于 2019-12-11 04:26:52
问题 I need to make a routine that counts bits in a word that does not involve loops (only bit operations), and does not use large constants. int x = 0xFFFFFFFF; x += (~((x >> 1) & 0x55555555)+1); x = (((x >> 2) & 0x33333333) + (x & 0x33333333)); x = (((x >> 4) + x) & 0x0F0F0F0F); x += (x >> 8); x += (x >> 16); return(x & 0x0000003F); This I found on bit twiddling hacks, but the largest constant I can use is 0xFF... Not sure how to do this otherwise. Thanks folks. 回答1: You can for example use a

How to determine which bit is set

给你一囗甜甜゛ 提交于 2019-12-11 03:29:52
问题 I have two bytes, they only differ in 1 bit. I want to know what bit this is. So: byte a,b; a=0; b=2; ChangedBit(a,b) should give bit 1 ChangedBit(4,5) should give bit 0 ChangedBit(7,3) should give bit 2 Any suggestions are very welcome!! Thanks, Erik 回答1: The correct solution would be to do var bit = Math.Log(a ^ b, 2); Although of course this leaves open the question of what happens if for any reason more than one bit is different. You could use var bit = (int)Math.Log(a ^ b, 2); to get you

Memory required to store 1000 telephone numbers

爱⌒轻易说出口 提交于 2019-12-11 03:28:39
问题 I was reading this question, but didn't understand some part of the aix's answer that how it requires only 17 bits to store 5 digits of the telephone numbers and how the total becomes 2128 bytes to store 1000 numbers. please help me with this silly doubt. Thanks in advance... 回答1: First 5 digits should be stored once, so since 5 digits means a number in 0-99999 in decimal base you need to store at least this in binary. Now the first binary value which allows you to fit 99999 numbers is 131072

bits to string python

巧了我就是萌 提交于 2019-12-11 03:22:51
问题 I have used this function to convert string to bits. def a2bits(chars): return bin(reduce(lambda x, y : (x<<8)+y, (ord(c) for c in chars), 1))[3:] How would I go about doing the reverse? Bits to string. Would I have to separate the bits into ASCII numbers and then convert them into characters? I got the function a2bits from this site: http://www.daniweb.com/software-development/python/code/221031/string-to-bits Is there something in the standard library to convert bits to string? 回答1: >>> def

c++ - Save the contents of a “std::vector<unsigned char>” to a file

旧城冷巷雨未停 提交于 2019-12-10 23:40:53
问题 I use the function below "writeFileBytes" to write the contents of a std::vector<unsigned char> to a file. I want to use the type "unsigned char" to save the file (note that is made a cast for "char"). The reason for that question is because "unsigned char" has compatibility with any byte ("00000000" bits, for example). When we use "char" we have some problems with the manipulation of certain "invalid" chars. See this topic What is the most suitable type of vector to keep the bytes of a file?

Bit Shift Large Binary File?

廉价感情. 提交于 2019-12-10 23:13:38
问题 What is the best or recommended method for bit shifting a large amount of binary data in C? I have a 200K binary file and I want to left, then right shift the entire lot. 回答1: If your OS can support it use a memory mapped file. Then do a bit shift It'll be very very efficient. See this answer for more info: What are the advantages of memory-mapped files? 来源: https://stackoverflow.com/questions/8309801/bit-shift-large-binary-file

Cast some light on population count algorithm

橙三吉。 提交于 2019-12-10 19:19:35
问题 I looked for good methods to do popcount (count of set bits). I found this one, here http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan unsigned int v; // count the number of bits set in v unsigned int c; // c accumulates the total bits set in v for(c = 0; v; c++) { v &= v - 1; // clear the least significant bit set } Trying on a few examples, its true that it works. What property of binary operations / representation makes it work? Could you hint at some further

Combining two bytes in Java

旧时模样 提交于 2019-12-10 18:45:08
问题 Having two bytes, how to make a new byte by taking the first 3 bits from the first byte and the last 5 from the second ? For instance, how would that for 11100000 and 00011111 ==> 11111111 ? I am using Java. 回答1: byte b1, b2; take first 3 bits: b1 & 0xE0 take last 5 bits: b2 & 0x1F concatenate: b1 | b2 回答2: You can use the BitSet class. There's an example in here. 回答3: Using the masks 0xE0 (11100000) and 0x1F (00011111), you can mask out the bits you don't want and bitwise or them together: