Questions tagged [bit]

A bit is a single binary digit.

A bit refers to a single binary digit, the smallest possible amount of information. In print is usually represented as either 0 or 1, with many different representations in technology, like the presence or absence of an electric current or magnetic field.

Eight bits form a single , although historically the term has been used for other sizes as well.

3593 questions
57
votes
18 answers

Why is number of bits always(?) a power of two?

We have 8-bit, 16-bit, 32-bit and 64-bit hardware architectures and operating systems. But not, say, 42-bit or 69-bit ones. Why? Is it something fundamental that makes 2^n bits a better choice, or is just about compatibility with existing systems?…
Joonas Pulakka
  • 36,252
  • 29
  • 106
  • 169
55
votes
4 answers

How to get the value of a bit at a certain position from a byte?

If I have a byte, how would the method look to retrieve a bit at a certain position? Here is what I have know, and I don't think it works. public byte getBit(int position) { return (byte) (ID >> (position - 1)); } where ID is the name of the…
Glen654
  • 1,102
  • 3
  • 14
  • 18
55
votes
6 answers

How to get binary representation of Int in Kotlin

Is there a method that can be used to get an Integer's representation in bits? For example when provided: 0 gives 0 4 gives 100 22 gives 10110
Rashi Karanpuria
  • 1,605
  • 1
  • 11
  • 18
54
votes
8 answers

How to read/write arbitrary bits in C/C++

Assuming I have a byte b with the binary value of 11111111 How do I for example read a 3 bit integer value starting at the second bit or write a four bit integer value starting at the fifth bit?
dtech
  • 47,916
  • 17
  • 112
  • 190
48
votes
14 answers

Convert bytes to bits in python

I am working with Python3.2. I need to take a hex stream as an input and parse it at bit-level. So I used bytes.fromhex(input_str) to convert the string to actual bytes. Now how do I convert these bytes to bits?
user904832
  • 557
  • 1
  • 6
  • 10
45
votes
7 answers

How many values can be represented with n bits?

For example, if n=9, then how many different values can be represented in 9 binary digits (bits)? My thinking is that if I set each of those 9 bits to 1, I will make the highest number possible that those 9 digits are able to represent. Therefore,…
Sean
  • 5,233
  • 5
  • 22
  • 26
44
votes
10 answers

Using Python How can I read the bits in a byte?

I have a file where the first byte contains encoded information. In Matlab I can read the byte bit by bit with var = fread(file, 8, 'ubit1'), and then retrieve each bit by var(1), var(2), etc. Is there any equivalent bit reader in python?
David
  • 443
  • 1
  • 4
  • 5
43
votes
4 answers

Byte to Binary String C# - Display all 8 digits

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 cut. Example: MyVeryOwnByte = 00001110 // Texbox shows -> 1110 MyVeryOwnByte = 01010101 // Texbox…
Hooch
  • 28,817
  • 29
  • 102
  • 161
42
votes
8 answers

How Does The Bitwise & (AND) Work In Java?

I was reading through some code examples and came across a & on Oracle's website on their Bitwise and Bit Shift Operators page. In my opinion it didn't do too well of a job explaining the bitwise &. I understand that it does a operation directly to…
Anzwur
  • 603
  • 1
  • 6
  • 10
42
votes
6 answers

How to get the true/false count from a bit field into two separate columns

I need to create a query that will sum the number of True(1) and False(0) into two separate columns from one bit field. I'm joining 3 tables and need it to be something like: Attribute | Class | Pass | Fail I will be grouping on Attribute and Class.
avgbody
  • 1,382
  • 3
  • 15
  • 31
42
votes
4 answers

How to modify bits in an integer?

I have an integer with a value 7 (0b00000111) And I would like to replace it with a function to 13 (0b00001101). What is the best algorithm to replace bits in an integer? For example: set_bits(somevalue, 3, 1) # What makes the 3rd bit to 1 in…
Váradi Norbert
  • 429
  • 1
  • 4
  • 3
41
votes
9 answers

Javascript - convert integer to array of bits

I am trying in javascript to convert an integer (which I know will be between 0 and 32), to an array of 0s and 1s. I have looked around but couldn't find something that works.. So, if I have an integer as 22 (binary 10110), I would like to access it…
DimC
  • 495
  • 1
  • 5
  • 12
39
votes
7 answers

How many bits is a "word"?

This is from the book Assembly Language Step By Step, Jeff Duntemann: Here’s the quick tour: A bit is a single binary digit, 0 or 1. A byte is 8 bits side by side. A word is 2 bytes side by side. A double word is 2 words side by side. A quad…
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
38
votes
5 answers

How to create an array of bits in Python?

How can I declare a bit array of a very large size, say 6 million bits?
zheric
  • 475
  • 2
  • 6
  • 10
37
votes
1 answer

What is an XOR sum?

I am not sure of the precise definition of this term. I know that a bitwise XOR operation is going bit by bit and taking the XOR of corresponding bits position wise. Is this result termed the 'XOR sum'? If not, what is an XOR sum, and how do you…
user2517777
  • 741
  • 2
  • 7
  • 9