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
1
vote
2 answers

Saving and loading bits/bytes in Python

I've been studying compression algorithms recently, and I'm trying to understand how I can store integers as bits in Python to save space. So first I save '1' and '0' as strings in Python. import os import numpy as np array= np.random.randint(0, 2,…
Moondra
  • 4,399
  • 9
  • 46
  • 104
1
vote
3 answers

C# - Bits and Bytes

I try to store some information in two bytes (byte[2]). In the first four bit of the first byte I want to store a "type-information" encoded as a value from 0-9. And in the last four bit + the second byte I want to store a size-info, so the maximum…
mMilk
  • 245
  • 1
  • 2
  • 10
1
vote
3 answers

C++ Compressing size of integer down to 2 bits?

I am doing a little game physics networking project right now, and I am trying to optimize the packets I am sending using this guide: https://gafferongames.com/post/snapshot_compression/ In the "Optimize Quaternions" section it says: Don’t always…
Schytheron
  • 715
  • 8
  • 28
1
vote
1 answer

How to write an amount of bits to a file that is not multiple of 8

I'm writing a java application which compresses text with a Huffman tree. The result I get is an array of ones and zeroes. Right now it's a byte[]. Only I want to write 8 ones and zeroes to a byte because else "01" would take up twice as much…
Tvde1
  • 1,246
  • 3
  • 21
  • 41
1
vote
2 answers

To clear bits from i through 0 - bit manipulation

I was going to Cracking the Coding Interview chapter 5 Bit manipulation and found way to clear bits from i through 0 in number num int mask = ~(-1 >>> (31 - i)); return num & mask Though the above works Can we make it simple like int mask = (-1 <<…
Harish Kayarohanam
  • 3,886
  • 4
  • 31
  • 55
1
vote
4 answers

Extracting hours, minutes and seconds from binary encoding

I wrote a program to solve the exercise below. I got my hours and minutes right, but I cannot get my seconds right. In order to save disk space Time field in the directory entry is 2 bytes long. Distribution of different bits which account for…
lyb
  • 33
  • 6
1
vote
3 answers

Setting a bit after using realloc

I am trying to fix my function where I set the Nth bit of an already allocated space. If the size of N is larger than the size of space malloc'd then reallocate more space to be able to set the Nth bit. My issue is that whenever I set a bit higher…
David
  • 11
  • 1
1
vote
4 answers

Why must I use the ~ operator when clearing a bit?

For example, if I want to set a bit in y at position n (in C) y = y | (1 << n) But if I want to delete a bit in y at position n I have to use the ~ operator after binary AND. y = y & ~(1 << n); My question: Why Must I use the ~ operator? Is this…
user9268897
1
vote
0 answers

How to set bit and print all bits in number using C

I have following function to print bits: void printBits(unsigned int num) { for(int bit=0;bit<(sizeof(unsigned int) * 8); bit++) { printf("%i ", num & 0x01); num = num >> 1; } } and I want to set bit number 5 as…
ElConrado
  • 1,477
  • 4
  • 20
  • 46
1
vote
1 answer

Why is the exponent in a denormalized float E = 1 - bias?

For example, with 32 bit denormalized floats, the exponent would be -126. What is the significance of -126 in this context?
Toby
  • 140
  • 6
1
vote
1 answer

How to cast uint8* to uint32

When writing: uint8_t var_a[4] = {0, 255, 0, 255}; I expect the memory layout to be: 00000000111111110000000011111111 But when casting to uint32: cout << *(uint32_t*)var_a; it returns 4278255360, which is the…
AndrouR
  • 23
  • 5
1
vote
1 answer

Rotate/Reverse positions of bits in 16-bit number

I am working with an 8051 microcontroller and have found a better way to transfer data to shift registers. Until now, I used the bit-banging technique shifting bits out one at a time starting with the most significant bit. The new method is done…
Mike -- No longer here
  • 2,064
  • 1
  • 15
  • 37
1
vote
4 answers

How to return middle bits?

I searched online and the common solution for returning x bits from start seems to be: mask = ((1 << x) - 1 ) << start then, mask & value. However, I'm confused on how this works still. If I have a number 0101 1100 and I want to return the two…
sy89
  • 195
  • 2
  • 14
1
vote
1 answer

Transform / rotate bits in byte array

I'm looking for a way to quickly rotate the bits in a byte array without massive for loops that gets and sets each bit from old to new position. In this particular circumstance I need to rotate by 90 degrees clockwise. So for instance if I…
1
vote
1 answer

C# convert bitmap to byte array for a led display

I have a led display i can send a byte array, one bit stands for an led. The display have 9216 leds. The byte array is 1152 bytes long (96 x 96 / 8). The first 12 bytes represent the top line, the next 12 bytes the second line,... I want work with…
live2
  • 3,771
  • 2
  • 37
  • 46