I want with a byte
treat the days of the week, that is to say that:
Lunes -> 1bit
Martes ->1bit
Miercoles ->1bit
Jueves ->1bit
Viernes ->1bit
Sabado ->1bit
Domingo ->1bit
Todos ->1bit
The thing is, I don't really know how to do it. It is clear that a byte
goes from 0
to 255
; What I want is, when I select a checkbox
, for example Monday and Tuesday, that bit 1 and bit 2 have a value and add that value to throw it into a field in the database called Dia
which is a smallint
...
That is, checkbox
Monday and Tuesday are active, so bit1 and bit2 have a value when I add them up and send it to the database with a query. My question is how to separate that byte into 8 bits to do this.
All the best!
In pascal, in general, you can use individual bitmasks and the binary operators
and
,or
,xor
to work with the individual bits within a Byte.Bitshift operators and can also be
shl
usedshr
.I usually declare constants to make my job easier, for example (counting bits 1 to 8 from right to left) 1
With this, you can set the individual bits of a variable, using the operator
or
, for example:You can also check if an individual bit is on or off, using the operator
and
:Finally, I would suggest, if for you each bit is a day of the week, use constants that help make the code more readable, say:
1 Be careful, someone with training in c, will ask that the bits go from 0 to 7, it is a matter of convention and it can be, in the end, in any way, as long as all the programmers involved in the project are informed of it .
There are several parts to this problem and maybe you should specify if you have problems in any of them.
checkbox
to a string (in binary format and vice versa); This is possibly the simplest."11100000"
in base10 (decimal) to store it in an integer of the Database; And also the reverse process.Here are a couple of procedures for the latter:
The result of using them will be something like this:
The number resulting from the conversion can already be stored in an integer in the Database (of the type you need) without problems.