bits

Python - trying to deal with the bits of a file

狂风中的少年 提交于 2019-12-25 01:33:28
问题 I have very recently started to learn Python, and I chose to learn things by trying to solve a problem that I find interesting. This problem is to take a file (binary or not) and encrypt it using a simple method, something like replacing every "1001 0001" in it with a "0010 0101", and vice-versa. However, I didn't find a way to do it. When reading the file, I can create an array in which each element contains one byte of data, with the read() method. But how can I replace this byte with

How to calculate total number of bits in BitSet object?

拥有回忆 提交于 2019-12-24 08:59:04
问题 I want to calculate the total number of bits in a BitSet object. The method length returns the "logical size" of the BitSet : the index of the highest set bit in the BitSet plus one, while method cardinality will give the total number of bits set to 1 in the object. I want to calculate the total number of bits including both 0s and 1s. How do I do that? 回答1: How about BitSet.size()?..... 回答2: The getObjectSize(Object o) method in the Instrumentation library is your guy: http://docs.oracle.com

How do I properly loop through and print bits of an Int, Long, Float, or BigInteger?

血红的双手。 提交于 2019-12-24 08:15:47
问题 I'm trying to debug some bit shifting operations and I need to visualize the bits as they exist before and after a Bit-Shifting operation. I read from this answer that I may need to handle backfill from the shifting, but I'm not sure what that means. I think that by asking this question (how do I print the bits in a int) I can figure out what the backfill is, and perhaps some other questions I have. Here is my sample code so far. static string GetBits(int num) { StringBuilder sb = new

reverse bits in byte ansi C

狂风中的少年 提交于 2019-12-24 07:08:22
问题 I have a function that reverse the bits in a byte but i don't understand the syntax. Why is used 0x0802U & 0x22110U and other binary operations(what are this numbers) unsigned char reverse(unsigned char B) { return (unsigned char)(((b * 0x0802U & 0x22110U) | (b * 0x8020U & 0x88440U)) * 0x10101U >> 16); } 回答1: Check the "Bit Twiddling Hacks" page for the explanation: Reverse the bits in a byte with 7 operations (no 64-bit) http://graphics.stanford.edu/~seander/bithacks.html 回答2: Direct link to

How to read file into array of Ints

随声附和 提交于 2019-12-23 05:27:57
问题 In my app documents folder I have a file which I am trying to read byte-per-byte into an array of UInt8 where each element represents a byte. How would I go about doing this? The file happens to be called Q1.dat. Here's my unsuccessful attempt: func readArray() -> [Int]? { if let arrayPath: String = createArrayPath() { if let arrayFromFile: [Int] = NSArray(contentsOfFile: arrayPath) as? [Int] { return arrayFromFile } } return nil } func createArrayPath () -> String? { if let docsPath: String

IP address input in C

时光怂恿深爱的人放手 提交于 2019-12-22 18:28:48
问题 I want to write a program in C which is taking input IP Address from user and then i want to do some bit operations on it. How can i take input in bits in C. I tried the below code but integer is of size 2 bytes, which makes the complete address here of 8 bytes(64 bits). When using char to scan input, its losing the value entered. Is there any way to take input in bits( i want 32 bits IPv4 address in 32 bits only and 128bit V6 in 128 bits only). unsigned short int a,b,c,d; scanf("%d.%d.%d.%d"

Swap bits in c++ for a double

三世轮回 提交于 2019-12-22 08:59:29
问题 Im trying to change from big endian to little endian on a double. One way to go is to use double val, tmp = 5.55; ((unsigned int *)&val)[0] = ntohl(((unsigned int *)&tmp)[1]); ((unsigned int *)&val)[1] = ntohl(((unsigned int *)&tmp)[0]); But then I get a warning: "dereferencing type-punned pointer will break strict-aliasing rules" and I dont want to turn this warning off. Another way to go is: #define ntohll(x) ( ( (uint64_t)(ntohl( (uint32_t)((x << 32) >> 32) )) << 32) | ntohl( ((uint32_t)(x

Why does this function count the number of set bits in an integer

僤鯓⒐⒋嵵緔 提交于 2019-12-21 20:18:48
问题 I was asked the following question in an interview: int foofoo(unsigned int u) { unsigned int foo = u; do{ u = u/2; foo -= u; }while(u > 0); return foo; } I was asked to tell what does this function do and I was able to find that it counts the number of set bits in an unsigned int value, but I was not able to prove that, maybe someone can? 回答1: I was able to find that it counts the number of set bits in an unsigned int value, but I was not able to prove that, maybe someone can? Look at a

Why does this function count the number of set bits in an integer

眉间皱痕 提交于 2019-12-21 20:18:09
问题 I was asked the following question in an interview: int foofoo(unsigned int u) { unsigned int foo = u; do{ u = u/2; foo -= u; }while(u > 0); return foo; } I was asked to tell what does this function do and I was able to find that it counts the number of set bits in an unsigned int value, but I was not able to prove that, maybe someone can? 回答1: I was able to find that it counts the number of set bits in an unsigned int value, but I was not able to prove that, maybe someone can? Look at a

Doubling and dividing floating point values

不羁岁月 提交于 2019-12-21 04:43:05
问题 I have the function which I believe will convert an int into a floating point value split into the sign exponent and fraction components of the value. Using IEEE 754 to represent Float values. unsigned test(unsigned x) { // split the given bits of sign exponent and fraction, combine to return unsigned int sign = (x & 0x80000000) >> 31; unsigned int expo = (x & 0x7F800000) >> 23; unsigned int frac = (x & 0x007fffff); return (sign << 31) | (expo << 23) | frac; } I'm unsure however how I could