473,395 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Permutations on binary strings

I am trying to write a program that encrypts 8-bit plaintext using a
10-bit key. To generate subkeys, and for other things, i will need to
be able to perform permutations on binary strings for example:

if k1k2k3k4k5k6k7k8k9k10 represents all ten digits of the key, the
outpur of the permutation should be like this:
k3k5k2k7k4k10k1k9k8k6 .

I am new to operations on binary strings and i don't really know how to
implement this as efficient as possible...

Nov 15 '05 #1
3 2735
ru***************@skynet.be wrote:
I am trying to write a program that encrypts 8-bit plaintext using a
10-bit key. To generate subkeys, and for other things, i will need to
be able to perform permutations on binary strings for example:

if k1k2k3k4k5k6k7k8k9k10 represents all ten digits of the key, the
outpur of the permutation should be like this:
k3k5k2k7k4k10k1k9k8k6 .

I am new to operations on binary strings and i don't really know how to
implement this as efficient as possible...


Then don't.

Implement it in a way you can understand, even if _seems_ terribly
slow to you. If you are sure you want to replace the respective
portions of the code later on, wrap repeating tasks in functions
or macros.
Afterwards, have a look at whether this is really terribly slow or
maybe fast enough. Do not try to go for the "most clever" variant
first. If you keep it simple enough, the compiler may even optimise
it better than you ever can using "clever" constructs.

Notes on working with bits:
- typical macros to work bitwise:
#define IS_BIT_SET(src, bit) ((src) & (1UL<<(bit)))
#define SET_BIT(src,bit) ((src) |= (1UL<<(bit)))
#define UNSET_BIT(src,bit) ((src) &= ~(1UL<<(bit)))
- Work with unsigned types.
- Beware of integer promotions to signed types.
- Similarly: Make sure to have the left operand of << to be of
the desired "destination" type
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #2
In article <11**********************@g49g2000cwa.googlegroups .com>,
<ru***************@skynet.be> wrote:
I am trying to write a program that encrypts 8-bit plaintext using a
10-bit key. To generate subkeys, and for other things, i will need to
be able to perform permutations on binary strings for example: if k1k2k3k4k5k6k7k8k9k10 represents all ten digits of the key, the
outpur of the permutation should be like this:
k3k5k2k7k4k10k1k9k8k6 . I am new to operations on binary strings and i don't really know how to
implement this as efficient as possible...


Really there are two distinct questions involved:
1) What is an efficient permutation algorithm; and
2) How do you work with individual bits

Efficient permutation algorithms are not generally considered
to be within the scope of this newsgroup -- but if I recall
correctly, then if you search the comp.lang.c newsgroup archives
(e.g., via google groups) then you will find some good permutation
algorithms (or references to such.)
With regards to working with bits:

You numbered the bits from MSB (Most Significant Bit) to LSB (Least
Significant Bit). Most often the bit order is numbered the other way
around and starts from 0, but people disagree about the "right" ordering --
and the ordering doesn't matter as long as you are consistant with
yourself and that if the ordering is visible to the users, that your
user interface makes it clear what the numbering system is.
The "key" that you are using, 10 bits long, is not a particularily
common size of byte, so you will need to embed the 10 bit key within a
wider integral type such as short (less space) or int (more natural for
arithmetic operations.) When you do that embedding, you need to decide
where the 10 bits are to be packed in the wider type -- left end (MSB
of the key at the MSB of the wider type), right end (LSB of the
key at the LSB of the wider type), or something else.

I would suggest to you that packing at the "right" end is
easier to deal with: that saves a lot of messiness about having
to know exactly how big the wider type is; and is
preserves interpretation of values if the word you are working with
happens to get promoted to a wider type yet.

If you pack at the "right" side of the wider type, and if you
use the numbering scheme you give (first bit is #1), then for
a W-bit wide quantity, you have the following:

#define SET_ON_BIT_N(x, W, N) (x | 1<<(W-N))
#define SET_OFF_BIT_N(x, W, N) (x & ~ 1<<(W-N))
#define GET_BIT_N(x, W, N) ((x & 1<<(W-N)) >> (W-N))
#define SET_BIT_N(x, W, N, s) (s ? SET_ON_BIT10_N(x, W, N) : \
SET_OFF_BIT10_N(x, W, N) )

If you want, you could define things like

#define SET_OFF_BIT10_N(x, N) SET_OFF_BIT_N(x, 10, N)

if you are going to always be using 10-bit wide keys.
You may notice that the code would simplify if you were numbering
the other way around, bit 9 as the first bit and bit 0 as the last.
--
The rule of thumb for speed is:

1. If it doesn't work then speed doesn't matter. -- Christian Bau
Nov 15 '05 #3
Walter Roberson wrote:
In article <11**********************@g49g2000cwa.googlegroups .com>,
<ru***************@skynet.be> wrote:
I am trying to write a program that encrypts 8-bit plaintext using a
10-bit key. To generate subkeys, and for other things, i will need to
be able to perform permutations on binary strings for example:
if k1k2k3k4k5k6k7k8k9k10 represents all ten digits of the key, the
outpur of the permutation should be like this:
k3k5k2k7k4k10k1k9k8k6 .

I am new to operations on binary strings and i don't really know how to
implement this as efficient as possible...

Really there are two distinct questions involved:
1) What is an efficient permutation algorithm; and
2) How do you work with individual bits

Efficient permutation algorithms are not generally considered
to be within the scope of this newsgroup -- but if I recall
correctly, then if you search the comp.lang.c newsgroup archives
(e.g., via google groups) then you will find some good permutation
algorithms (or references to such.)
With regards to working with bits:

You numbered the bits from MSB (Most Significant Bit) to LSB (Least
Significant Bit). Most often the bit order is numbered the other way
around and starts from 0, but people disagree about the "right" ordering --
and the ordering doesn't matter as long as you are consistant with
yourself and that if the ordering is visible to the users, that your
user interface makes it clear what the numbering system is.
The "key" that you are using, 10 bits long, is not a particularily
common size of byte, so you will need to embed the 10 bit key within a
wider integral type such as short (less space) or int (more natural for
arithmetic operations.) When you do that embedding, you need to decide
where the 10 bits are to be packed in the wider type -- left end (MSB
of the key at the MSB of the wider type), right end (LSB of the
key at the LSB of the wider type), or something else.


Note: Working with a signed type and bits requires extra care.
So I would rather suggest unsigned integer types.
I would suggest to you that packing at the "right" end is
easier to deal with: that saves a lot of messiness about having
to know exactly how big the wider type is; and is
preserves interpretation of values if the word you are working with
happens to get promoted to a wider type yet.

If you pack at the "right" side of the wider type, and if you
use the numbering scheme you give (first bit is #1), then for
a W-bit wide quantity, you have the following:

#define SET_ON_BIT_N(x, W, N) (x | 1<<(W-N))
#define SET_OFF_BIT_N(x, W, N) (x & ~ 1<<(W-N))
#define GET_BIT_N(x, W, N) ((x & 1<<(W-N)) >> (W-N))
#define SET_BIT_N(x, W, N, s) (s ? SET_ON_BIT10_N(x, W, N) : \
SET_OFF_BIT10_N(x, W, N) )
Caveat: This can easily break as you neglected putting
parentheses wherever appropriate/applicable.
Apart from that, I prefer 1UL/1L as left hand side argument to
<< to make sure that I am on the safe side. (Undefined behaviour
by shifting by 16 or more for 16-bit-ints is easily avoided by
this.)
If x is signed and we have arithmetical right shift, your
GET_BIT_N macro does not work as intended. First >> then mask.
In SET_BIT_N(x, W, N, s) you used SET_ON_BIT10_N/SET_OFF_BIT10_N
instead of SET_ON_BIT_N/SET_OFF_BIT_N as probably intended.

BTW: I would not really go about the above in that way;
W does not really introduce something new and useful, so I would
rather define two macros:

#define SET_ON_BIT_N(x, N) ((x) | 1<<(N))
#define SET_ON_BIT_W_N(x, W, N) SET_ON_BIT_N(x,(W)-(N))

If you want, you could define things like

#define SET_OFF_BIT10_N(x, N) SET_OFF_BIT_N(x, 10, N)

if you are going to always be using 10-bit wide keys.
You may notice that the code would simplify if you were numbering
the other way around, bit 9 as the first bit and bit 0 as the last.


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jeff Kish | last post by:
Hi. I realize this might be more of a "figure out the algorithm" thing than strictly an std question.. sorry if it is off topic? It certainly was in the other group! Also, I'm pretty old, not...
13
by: Kiran Dalvi | last post by:
Hi, Can anybody please suggest me an efficient approach to find out all possible permutations of a String. e.g. My input string = "ABC". My program should give an output like .... ABC, ACB, BAC,...
3
by: jason.s.turner | last post by:
I have spent hours on this problem and cannot get it, any help would be appreciated: A binary search tree using n distinct integers 1, 2, ..., n. There are n! possible initial orderings of the...
1
by: Jose | last post by:
Hi all, I have a select list that can contain between 1 and N items. I would like to get all possible permutations of the list, i.e. combinations that a user might select. So for example if my...
11
by: Girish Sahani | last post by:
Hi guys, I want to generate all permutations of a string. I've managed to generate all cyclic permutations. Please help :) def permute(string): l= l.append(string) string1 = '' for i in...
20
by: anurag | last post by:
hey can anyone help me in writing a code in c (function) that prints all permutations of a string.please help
9
by: =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?= | last post by:
With regexps you can search for strings matching it. For example, given the regexp: "foobar\d\d\d". "foobar123" would match. I want to do the reverse, from a regexp generate all strings that could...
1
by: JosAH | last post by:
Greetings, last week we talked a bit about generating permutations and I told you that this week will be about combinations. Not true; there's a bit more to tell about permutations and that's...
5
by: Pablo Torres | last post by:
Hey guys! For the last couple of days, I've been fighting a war against generators and they've beaten the crap out of me several times. What I want to do is implement one that yields every...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.