bits

Byte to Binary String C# - Display all 8 digits

≡放荡痞女 提交于 2019-12-17 16:26:53
问题 I want to display one byte in textbox. Now I'm using: Convert.ToString(MyVeryOwnByte, 2); But when byte is has 0's at begining those 0's are being cuted. Example: MyVeryOwnByte = 00001110 // Texbox shows -> 1110 MyVeryOwnByte = 01010101 // Texbox shows -> 1010101 MyVeryOwnByte = 00000000 // Texbox shows -> <Empty> MyVeryOwnByte = 00000001 // Texbox shows -> 1 I want to display all 8 digits. 回答1: Convert.ToString(MyVeryOwnByte, 2).PadLeft(8, '0'); This will fill the empty space to the left

float bits and strict aliasing

我怕爱的太早我们不能终老 提交于 2019-12-17 09:34:01
问题 I am trying to extract the bits from a float without invoking undefined behavior. Here is my first attempt: unsigned foo(float x) { unsigned* u = (unsigned*)&x; return *u; } As I understand it, this is not guaranteed to work due to strict aliasing rules, right? Does it work if a take an intermediate step with a character pointer? unsigned bar(float x) { char* c = (char*)&x; unsigned* u = (unsigned*)c; return *u; } Or do I have to extract the individual bytes myself? unsigned baz(float x) {

Efficient bitwise operations for counting bits or find the right|left most ones

夙愿已清 提交于 2019-12-17 06:49:10
问题 Given an unsigned int, I have to implement the following operations : Count the number of bits set to 1 Find the index of the left-most 1 bit Find the index of the righ-most 1 bit (the operation should not be architecture dependents). I've done this using bitwise shift, but I have to iterate through almost all the bits(es.32) . For example, counting 1's: unsigned int number= ...; while(number != 0){ if ((number & 0x01) != 0) ++count; number >>=1; } The others operation are similar. So my

How to swap byte nibbles? [duplicate]

痴心易碎 提交于 2019-12-13 08:45:39
问题 This question already has answers here : Swapping the nibbles in a char element [duplicate] (3 answers) Closed 3 years ago . Given these bytes (hexadecimal representation): 0F 1A 2C how can I get: F0 A1 C2 ? 回答1: Use bitwise operators. ((x & 0x0f) << 4) | ((x & 0xf0) >> 4) 回答2: You can do it like this: ((x & 0x0f) << 4 ) | ((( x & 0xf0) >> 4) & 0xf ) This looks a lot like Josh Kelley's answer, but Josh's answer is wrong. Here's why: #include <stdio.h> int main( int argc, char *argv[] ) {

simple wav 16-bit / 8-bit converter source code?

拜拜、爱过 提交于 2019-12-13 07:00:51
问题 could someone provide me a link to source code (preferably C/C++) of a simple WAV 16-bit to 8-bit (and back if possible) converter? I have basic knowledge of C++ and I need to have a resource to understand wav writing and converting values for my new project. At this moment I have a lecture about RIFF chunks structure. Also any formulas for converting values between different bit depths (also these un standard) appreciated... I'll prefer it to be simple and command line so I can edit it with

Why is the smallest value that can be stored is a Byte(8bit) & not a Bit(1bit)?

老子叫甜甜 提交于 2019-12-12 12:31:02
问题 Why is the smallest value that can be stored a Byte(8bit) & not a Bit(1bit) in memory? Even booleans are stored as Bytes. Will we ever bump the smallest number to 32 or 64bits like register's on the CPU? 回答1: The underlying methods of processor access are limited to the size of the smallest usable register. On most architectures, that size is 8 bits. You can use smaller portions of these; for instance, C has the bitfield feature in structs that will allow combining fields that only need to be

Looking for a clear and concise web page explaining why lower bits of random numbers are usually not that random

狂风中的少年 提交于 2019-12-12 12:24:40
问题 I am putting together an internal "every developer should know" wiki page. I saw many discussions regarding rand() % N , but not a single web page that explains it all. For instance, I am curious if this problem is only C- and Linux-specific, or if it also applies to Windows, C++,. Java, .Net, Python, Perl. Please help me get to the bottom of this. Also, just how non-random do the numbers get? Thank you! 回答1: I don't have a web page to refer you to but I might have a "back of the envelope"

Can somebody explain the following xor property

半世苍凉 提交于 2019-12-12 12:05:29
问题 I a forum it is mentioned that given array of n numbers: arr[0........n-1] The following conditon holds, ^ is the xor operator ` f(l,r) = f(0,r) ^ f(0,l-1) where f(l,r) = arr[l]^arr[l+1]^........arr[r] I checked the above taking number of arrays and different values of l and r and YES , it is true. But I don't understand how? Can somebody please explain the logic behind this? 回答1: Use the simplest property of XOR f(0,r) ^ f(0,l-1) = f(l,r) => (f(0,r) ^ f(0,l-1)) ^ f(0,l-1) = f(0,l-1) ^ f(l,r)

Bits to Byte in C++

假如想象 提交于 2019-12-12 06:45:45
问题 I'm trying to: convert a group of 8 integers, all of value 0 or 1, into a byte reverse the bit order of that byte print the value of that byte (in what format?) ( i can guess until i have it right here ) Also, I'm not allowed to use the STL for this problem. 回答1: So, you want to reverse the bits in a byte. That is, the bits should move so: from: 7 6 5 4 3 2 1 0 to: 0 1 2 3 4 5 6 7 This code will do it, inelegantly - you can find much better algorithms if you search. Can you see how it works

Inspecting the LPARAM on WM_KEYDOWN - incorrect values?

自作多情 提交于 2019-12-12 01:48:41
问题 I have some simple code that inspects the LPARAM variable(sent to the main WNDPROC) values(bits) when the WM_KEYDOWN is received. But I am getting some funny values int there: In MSDN, http://msdn.microsoft.com/en-us/library/ms646280(v=vs.85).aspx, it says that the last bit(of LPARAM) should always be 0 for a keydown message but when I output the LPARAM value its ALWAYS 1? Also the scan code only ever alters between 5(when I press an arrow or windows key) or zero for normal letters & numbers