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
335
votes
11 answers

max value of integer

In C, the integer (for 32 bit machine) is 32 bits, and it ranges from -32,768 to +32,767. In Java, the integer(long) is also 32 bits, but ranges from -2,147,483,648 to +2,147,483,647. I do not understand how the range is different in Java, even…
stackuser
  • 4,081
  • 4
  • 21
  • 27
185
votes
8 answers

Imply bit with constant 1 or 0 in SQL Server

Is it possible to express 1 or 0 as a bit when used as a field value in a select statement? e.g. In this case statement (which is part of a select statement) ICourseBased is of type int. case when FC.CourseId is not null then 1 else 0 end as…
Damien McGivern
  • 3,954
  • 3
  • 27
  • 21
121
votes
6 answers

What is the difference between BIT and TINYINT in MySQL?

In which cases would you use which? Is there much of a difference? Which I typically used by persistence engines to store booleans?
carrier
  • 32,209
  • 23
  • 76
  • 99
97
votes
7 answers

C/C++: Force Bit Field Order and Alignment

I read that the order of bit fields within a struct is platform specific. What about if I use different compiler-specific packing options, will this guarantee data is stored in the proper order as they are written? For example: struct Message { …
dewald
  • 5,133
  • 7
  • 38
  • 42
72
votes
11 answers

What USEFUL bitwise operator code tricks should a developer know about?

I must say I have never had cause to use bitwise operators, but I am sure there are some operations that I have performed that would have been more efficiently done with them. How have "shifting" and "OR-ing" helped you solve a problem more…
non sequitor
  • 18,296
  • 9
  • 45
  • 64
69
votes
16 answers

Bitwise operator for simply flipping all bits in an integer?

I have to flip all bits in a binary representation of an integer. Given: 10101 The output should be 01010 What is the bitwise operator to accomplish this when used with an integer? For example, if I were writing a method like int flipBits(int…
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
68
votes
7 answers

Minimum bit length needed for a positive integer in Python

1 = 0b1 -> 1 5 = 0b101 -> 3 10 = 0b1010 -> 4 100 = 0b1100100 -> 7 1000 = 0b1111101000 -> 10 … How can I get the bit length of an integer, i.e. the number of bits that are necessary to represent a positive integer in Python?
user288832
  • 929
  • 2
  • 7
  • 9
68
votes
14 answers

Generate all binary strings of length n with k bits set

What's the best algorithm to find all binary strings of length n that contain k bits set? For example, if n=4 and k=3, there are... 0111 1011 1101 1110 I need a good way to generate these given any n and any k so I'd prefer it to be done with…
kevmo314
  • 4,223
  • 4
  • 32
  • 43
67
votes
13 answers

Implement division with bit-wise operator

How can I implement division using bit-wise operators (not just division by powers of 2)? Describe it in detail.
TimeToCodeTheRoad
  • 7,032
  • 16
  • 57
  • 70
66
votes
3 answers

What does (number & -number) mean in bit programming?

For example: int get(int i) { int res = 0; while (i) { res = (res + tree[i]) % MOD; i -= ( (i) & (-i) ); } return res; } A tree update function: void update(int i, int val) { while (i <= m) { tree[i] =…
SwadhIn
  • 717
  • 1
  • 9
  • 19
63
votes
7 answers

Can't see MySQL BIT field value when using SELECT

my_table contains the enabled field which is defined as: enabled BIT NOT NULL DEFAULT 0. This table has multiple rows with enabled = b'0', and multiple rows with enabled = b'1'. However, both this: SELECT * from my_table WHERE enabled = b'0'; and…
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
61
votes
9 answers

Divide by 10 using bit shifts?

Is it possible to divide an unsigned integer by 10 by using pure bit shifts, addition, subtraction and maybe multiply? Using a processor with very limited resources and slow divide.
Thomas O
  • 6,026
  • 12
  • 42
  • 60
60
votes
4 answers

Really 1 KB (KiloByte) equals 1024 bytes?

Until now I believed that 1024 bytes equals 1 KB (kilobyte) but I was reading on the internet about decimal and binary system. So, actually 1024 bytes = 1 KB would be the correct way to define or simply there is a general confusion?
SamYan
  • 1,553
  • 1
  • 19
  • 38
59
votes
11 answers

Switch on Enum (with Flags attribute) without declaring every possible combination?

how do i switch on an enum which have the flags attribute set (or more precisely is used for bit operations) ? I want to be able to hit all cases in a switch that matches the values declared. The problem is that if i have the following…
MartinF
  • 5,929
  • 5
  • 40
  • 29
57
votes
3 answers

The difference between logical shift right, arithmetic shift right, and rotate right

I've been reading the classic Hacker's delight and I am having trouble understanding the difference between logical shift right,arithmetic shift right, and rotate right. Please excuse if the doubt seems too simple.
Chandrahas Aroori
  • 955
  • 2
  • 14
  • 27
1
2 3
99 100